gpt4 book ai didi

ios - 如何将新的 UICollectionViewController 推送到单元格单元格内的导航 Controller

转载 作者:行者123 更新时间:2023-11-28 10:34:59 24 4
gpt4 key购买 nike

我以编程方式构建了一个返回 4 个单元格的 UICollectionViewController。 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)

}



}

让我们以 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")
}
}

在 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

}

如果我单击该元素,我的目标是将一个新的 ViewController 推送到 navigationController。但我无权访问,也不知道如何更改该嵌套结构中的 View 。我尝试了 HomeCell 类中的 didSelectItem 方法,在单击第三个单元格时能够在控制台打印一些内容,但无法更改 View 。

class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 2 {

print(123)

}

....

}

请帮忙。有没有办法在 HomeCell 的 didSelectItem 方法中更改 View ??

最佳答案

您需要编写一个协议(protocol)来引用您的 HomeController。

protocol HomeCellProtocol {
func pushNavigation(_ vc: UIViewController)
}

将写入委托(delegate)属性添加到 HomeCell 类中

class HomeCell: ..... {
var delegate: HomeCellProtocol?
}

并让 HomeController 通过 HomeCellProtocol 确认

extention HomeController: HomeCellProtocol {
func pushNavigation(_ vc: UIViewController) {
self.navigationController?.pushViewController(vc, animated: true)
}
}

当你设置 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
}

最后,您可以在 HomeCell 中调用推送功能

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 2 {
let vc = UIViewController();
self.delegate?.pushNavigation(vc);
}
}

关于ios - 如何将新的 UICollectionViewController 推送到单元格单元格内的导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54057441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com