UICollectionViewController 调用另一个 UICollectionViewController 的 DataSource 方法
UICollectionViewController Calling DataSource method of another UICollectionViewController
我已经实现了自定义 UITabBarController
第一和第二 UITabBarItem
是 UICollectionViewController
第四个UITabBarItem
是一个UIViewController
第一个选项卡中的 CollectionView 及其单元格是以编程方式创建的,并且与动态单元格大小完美配合。
第二个选项卡中的 CollectionView 方法未编辑。
问题:应用程序启动并且第一个 CollectionView 加载了一个单元格,然后我转到第二个选项卡,它也是一个没有任何单元格的 CollectionViewController,然后我转到再次显示第一个选项卡,第一个 UICollectionview 上的单元格将不会显示。
我注意到的事情:
- 在两个 VC 中的
collectionview
方法上添加了调试点。在我从第二个选项卡移动到第一个选项卡后,第一个选项卡 VC 正在调用第二个 VC(奇怪!)的方法。
- 转到第四个选项卡 (
UIViewController
),然后返回到第一个选项卡不会造成任何问题。
我试过的解决方法:
- 在
viewWillAppear
中注册第一个 VC 的单元格(它曾经在 dequeueReusableCell
时崩溃,但现在在分配标签并在 cellForItemAt 中检查它后不会崩溃)
- 将标记分配给 VC 中的
collectionview
并在 VC 的方法中检查 collectioniView.tag
,但第一个选项卡 VC 仍在调用第二个选项卡中的方法 VC
编辑 1:
在 viewWillAppear()
中调用
SetupCollectionView(){
collectionView?.register(VideoCell.self, forCellWithReuseIdentifier: cellId)
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
}
第一个 VC 中的 CollectionView 方法
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 339 {
print("videos.count: \(self.videos.count)")
if videos.count > 0 {
return self.videos.count
}else {
return 0
}
}
return 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 339 {
//setup cell to display
}else{
let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
return cell2
}
}
在 sizeForItemAt
和第二个 VC 中执行相同的检查。
编辑 2:
在 AppDelegate 中
self.window?.rootViewController = CustomTabBarController()
在 CustomTabBarController
第一 VC
let layout = UICollectionViewFlowLayout()
let homeController = HomeControllerMain(collectionViewLayout: layout)
let homeViewNavController = UINavigationController(rootViewController: homeController)
homeViewNavController.tabBarItem.title = nil
homeViewNavController.tabBarItem.image = UIImage(named: "home1")
homeViewNavController.tabBarItem.selectedImage = UIImage(named: "home")
homeViewNavController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
第二VC
let commentController = searchMainViewController(collectionViewLayout: layout)
let commentViewNavController = UINavigationController(rootViewController: commentController)
commentViewNavController.tabBarItem.title = ""
commentViewNavController.tabBarItem.image = UIImage(named: "search1")
commentViewNavController.tabBarItem.selectedImage = UIImage(named: "search")?.withRenderingMode(.alwaysOriginal)
commentViewNavController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
已解决!
我在 homeController 和 commentController 中都使用了单个 UICollectionViewFlowLayout
变量。
为 commentController 创建了第二个 UICollectionViewFlowLayout
变量并解决了问题。
我已经实现了自定义 UITabBarController
第一和第二 UITabBarItem
是 UICollectionViewController
第四个UITabBarItem
是一个UIViewController
第一个选项卡中的 CollectionView 及其单元格是以编程方式创建的,并且与动态单元格大小完美配合。
第二个选项卡中的 CollectionView 方法未编辑。
问题:应用程序启动并且第一个 CollectionView 加载了一个单元格,然后我转到第二个选项卡,它也是一个没有任何单元格的 CollectionViewController,然后我转到再次显示第一个选项卡,第一个 UICollectionview 上的单元格将不会显示。
我注意到的事情:
- 在两个 VC 中的
collectionview
方法上添加了调试点。在我从第二个选项卡移动到第一个选项卡后,第一个选项卡 VC 正在调用第二个 VC(奇怪!)的方法。 - 转到第四个选项卡 (
UIViewController
),然后返回到第一个选项卡不会造成任何问题。
我试过的解决方法:
- 在
viewWillAppear
中注册第一个 VC 的单元格(它曾经在dequeueReusableCell
时崩溃,但现在在分配标签并在 cellForItemAt 中检查它后不会崩溃) - 将标记分配给 VC 中的
collectionview
并在 VC 的方法中检查collectioniView.tag
,但第一个选项卡 VC 仍在调用第二个选项卡中的方法 VC
编辑 1:
在 viewWillAppear()
SetupCollectionView(){
collectionView?.register(VideoCell.self, forCellWithReuseIdentifier: cellId)
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
}
第一个 VC 中的 CollectionView 方法
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 339 {
print("videos.count: \(self.videos.count)")
if videos.count > 0 {
return self.videos.count
}else {
return 0
}
}
return 0
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 339 {
//setup cell to display
}else{
let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
return cell2
}
}
在 sizeForItemAt
和第二个 VC 中执行相同的检查。
编辑 2:
在 AppDelegate 中
self.window?.rootViewController = CustomTabBarController()
在 CustomTabBarController
第一 VC
let layout = UICollectionViewFlowLayout()
let homeController = HomeControllerMain(collectionViewLayout: layout)
let homeViewNavController = UINavigationController(rootViewController: homeController)
homeViewNavController.tabBarItem.title = nil
homeViewNavController.tabBarItem.image = UIImage(named: "home1")
homeViewNavController.tabBarItem.selectedImage = UIImage(named: "home")
homeViewNavController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
第二VC
let commentController = searchMainViewController(collectionViewLayout: layout)
let commentViewNavController = UINavigationController(rootViewController: commentController)
commentViewNavController.tabBarItem.title = ""
commentViewNavController.tabBarItem.image = UIImage(named: "search1")
commentViewNavController.tabBarItem.selectedImage = UIImage(named: "search")?.withRenderingMode(.alwaysOriginal)
commentViewNavController.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
已解决!
我在 homeController 和 commentController 中都使用了单个 UICollectionViewFlowLayout
变量。
为 commentController 创建了第二个 UICollectionViewFlowLayout
变量并解决了问题。