【问题标题】:UICollectionViewController Calling DataSource method of another UICollectionViewControllerUICollectionViewController 调用另一个 UICollectionViewController 的 DataSource 方法
【发布时间】:2018-06-18 06:37:33
【问题描述】:

我已经实现了自定义UITabBarController
第一个和第二个 UITabBarItemUICollectionViewController
第四个UITabBarItemUIViewController

CollectionView 在第一个选项卡中,其单元格以编程方式创建,并且与动态单元格大小完美配合。

第二个选项卡中的CollectionView 方法未被编辑。

问题: 应用程序启动并且第一个 CollectionView 加载了一个单元格,然后我转到第二个选项卡,它也是一个没有任何单元格的 CollectionViewController,然后我再次转到第一个选项卡,然后第一个 UICollectionview 上的单元格不会显示。

我注意到的事情:

  1. 在两个 VC 中的 collectionview 方法上添加了调试点。在我从第二个选项卡移动到第一个选项卡后,第一个选项卡 VC 正在调用第二个 VC(奇怪!)的方法。
  2. 转到第四个选项卡 (UIViewController) 然后返回到第一个选项卡不会导致任何问题。

我尝试过的解决方案:

  1. viewWillAppear注册第一个VC的单元格(它曾经在dequeueReusableCell崩溃,但现在在分配标签并在cellForItemAt中检查后不会崩溃)
  2. 在两个 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:

  1. 在 AppDelegate 中

    self.window?.rootViewController = CustomTabBarController()

  2. 在 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)

第二个风投

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)

【问题讨论】:

  • 你应该包含你的代码
  • 请指定你想看哪个vc的代码?
  • 所涉及的所有内容.. 但相关代码就足够了(连接collectionViews及其单元格等)
  • 添加了我正在使用的代码
  • 尝试在第一个 vc 中设置调试器,因为它似乎 numberOfItems 返回 0。也在您的单元格中为 item 方法:“如果 collectionView.tag == 339 { //setup cell to display }”,你是不返回任何单元格,这可能是崩溃的关键。用调试器检查

标签: ios swift3 uicollectionview


【解决方案1】:

解决了!
我在 homeControllercommentController 中都使用了单个 UICollectionViewFlowLayout 变量。

commentController 创建了第二个 UICollectionViewFlowLayout 变量并解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多