【发布时间】:2021-01-14 20:01:59
【问题描述】:
我有PhotosGridController,它只添加了一个collectionView 作为它的视图的子视图。这个 Viewcontroller 拥有它的 collectionView 的所有委托和数据源,所以,我的代码变得更干净了。
我还有一个MainPageController,它有另一个collectionView(主要的),在其中,它是一个自定义单元格(名为MainPageCell)。这个单元格有一个名为imageViewGrid 的UIView,它的目的是显示PhotoGridController 的视图。但这里有一个错误:当我在添加 PhotosGridController 作为 MainPageController 的 childController 之后运行我的代码,并将它的视图添加为 imageViewGrid 子视图时,在显示的单元格上没有正确加载 .实际上,当我向下/向上滚动时,我可以看到所需的 collectionView 几乎一秒钟,当我停止滚动时,它又消失了。
MainPage collectionView 的数据源:
extension MainPageController {
. . .
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MainPageCell
displayController(contentController: photoGridController, on: cell.imageViewGrid)
cell.layoutIfNeeded()
return cell
}
}
MainPage displayController 方法:
extension MainPageController {
func displayController(contentController content: UIViewController, on view: UIView) {
self.addChild(content)
view.addSubview(content.view)
content.didMove(toParent: self)
content.view.layoutIfNeeded()
content.view.becomeFirstResponder()
}
}
PhotoGrid 数据源:
extension PhotosGridController {
. . .
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoGridCell
cell.backgroundColor = Colors.lightBlue
cell.item = names[indexPath.item]
return cell
}
. . .
}
我认为问题出在 MainPage 上的 cellForItemAt 方法上,但我不知道如何解决。如果您需要更多代码,请告诉我。
注意:如果我将 PhotosGridController 设置为 SceneDelegate 上的 rootViewController,它会完美加载并在模拟器上显示所需的屏幕。
【问题讨论】:
标签: ios swift uicollectionviewcell