Dev*_*ari 0 uinavigationcontroller ios uicollectionviewcell uicollectionviewdelegate swift
我以编程方式构建了一个 UICollectionViewController,它返回 4 个单元格。HomeCell、TrendingCell、SubscriptionCell 和 AccountCell。所有 4 个单元格应该不同,您可以水平滚动它们。<--->。
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout{
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId)
collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId)
collectionView?.register(AccountCell.self, forCellWithReuseIdentifier: accountCellId)
}
}Run Code Online (Sandbox Code Playgroud)
让我们以 HomeController 的第一个单元(称为 HomeCell)来说明我的问题。Homecell 具有三个自定义单元,称为 VideoCell、CategoryCell 和 UserSearchCell。
class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
let searchId = "sarchId"
let scrollId = "scrollId"
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.dataSource = self
cv.delegate = self
return cv
}()
override func setupViews() {
super.setupViews()
.....
// register three different cells within HomeCell
collectionView.register(VideoCell.self, forCellWithReuseIdentifier: cellId)
collectionView.register(CategoryCell.self, forCellWithReuseIdentifier: scrollId)
collectionView.register(UserSearchCell.self, forCellWithReuseIdentifier: searchId) //
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Run Code Online (Sandbox Code Playgroud)
在 HomeCell 中,我将 UserSearchCell 注册为第三个单元格。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoId", for: indexPath)
} else if indexPath.item == 1{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: categoryId, for: indexPath)
}else {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: searchId, for: indexPath)
}
return cell
}Run Code Online (Sandbox Code Playgroud)
如果我单击该元素,我的目标是将新的 ViewController 推送到 navigationController。但我无权访问,也不知道如何更改该嵌套结构中的视图。我尝试了 HomeCell 类中的 didSelectItem 方法,并且在单击第三个单元格时能够在控制台打印一些内容,但无法更改视图。
class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 2 {
print(123)
}
....
}Run Code Online (Sandbox Code Playgroud)
请帮忙。有没有办法在 HomeCell 的 didSelectItem 方法中更改视图?
小智 5
您需要编写一个协议来引用您的 HomeController。
protocol HomeCellProtocol {
func pushNavigation(_ vc: UIViewController)
}
Run Code Online (Sandbox Code Playgroud)
将写入委托属性添加到 HomeCell 类
class HomeCell: ..... {
var delegate: HomeCellProtocol?
}
Run Code Online (Sandbox Code Playgroud)
并使 HomeController 确认 HomeCellProtocol
extention HomeController: HomeCellProtocol {
func pushNavigation(_ vc: UIViewController) {
self.navigationController?.pushViewController(vc, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
当您设置 HomeCell 时,您需要在 HomeController 中设置您的委托
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: homeCellId, for: indexPath) as HomeCell;
cell.delegate = self // Set the delegate
return cell
}
Run Code Online (Sandbox Code Playgroud)
最后,您可以在 HomeCell 中调用推送函数
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 2 {
let vc = UIViewController();
self.delegate?.pushNavigation(vc);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
959 次 |
| 最近记录: |