UICollectionView numberOfItemsInSection 被调用两次 [英] UICollectionView numberOfItemsInSection being called twice

查看:41
本文介绍了UICollectionView numberOfItemsInSection 被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 viewController 中启用分页的 scrollView,我想要 6 个集合视图,这些集合视图是滚动视图的子视图.collectionViews 中有不同数量的项目.

I have a scrollView with paging enabled in my viewController and I want to have 6 collection views and these collectionViews are subviews of the scroll view. The collectionViews have different number of items in them.

viewDidLoad 看起来像这样-

The viewDidLoad looks like this-

override func viewDidLoad() {
    super.viewDidLoad()
    //Set the frame for scroll view
    //Programatically created 6 collection views and added them to scrollView
    //Set the content size for scroll view 

}

每个 collectionView 都与 viewDidLoad 中的一个标签相关联.

Each collectionView is associated with a tag in the viewDidLoad.

collectionView1.tag = 0
collectionView2.tag = 1 and so on..

And the collectionViews were added to the scroll view serially starting from collectionView with tag 0

let noOfItemsArray = [2, 4, 6, 3 ,8, 7] 
// 1st collection view has 2 items, 2nd has 4 and so on..

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return noOfItemsArray[collectionView.tag]
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
    cell.backgroundColor = UIColor.redColor()
    return cell
}

应用程序崩溃.所以为了调试,我将 numberOfItemsInSection 修改为 -

The app crashes. So for debugging, I modified my numberOfItemsInSection to -

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print(collectionView.tag)
    return 10
}

我发现 numberOfItemsInSection 首先为带有标签 5 的 collectionView 调用.为这个 collectionView 调用了一次.但是对于其余的 collectionViews,numberOfItemsInSection 被调用了两次 - 第一次调用 collectionView.tag = 5,然后在第二次调用中,collectionView.tag = 4(3,2,1 等..)

I found that numberOfItemsInSection was called first for collectionView with tag 5. It was called once for this collectionView. But for the rest of the collectionViews, numberOfItemsInSection was being called twice - in first call collectionView.tag = 5, and then in second call, collectionView.tag = 4 ( 3,2,1 and so on..)

 Output was - 
 5 //For collectionView with tag 5, called only once
 5 //For collectionView with tag 4, the first call to numberOfItemsInSection
 4 //For collectionView with tag 4, the second call to numberOfItemsInSection, and so on..
 5
 3
 5
 2
 5
 1
 5
 0

现在,由于 numberOfItemsInSection 总是返回 10,我可以在每个 Collectionview 中看到 10 个项目.但早些时候,当我返回 noOfItemsArray[collectionView.tag] 时,由于 2 个调用返回不同的值,我的应用程序崩溃了.

Now since numberOfItemsInSection was always returning 10, I could see 10 items in each of my Collectionview. But earlier, when i was returning noOfItemsArray[collectionView.tag], since the 2 calls were returning different values, my app crashed.

为什么会发生这种情况,最好的解决方案是什么?

Why is this happening and what is the best possible solution to it?

感谢您抽出宝贵时间.

推荐答案

我刚刚和你遇到了同样的问题.最后,我发现我的问题的原因是我对 collectionViews 使用了相同的布局.为每个 collectionView 创建布局后,numberOfItemsInSection 只会为每个 collectionView 调用一次.希望有所帮助.

I just met the same issue with you. At last, I found the cause of my problem is that I used the same layout for the collectionViews. After creating a layout for each collectionView, numberOfItemsInSection will only called once for each collectionView. Hope that helps.

这篇关于UICollectionView numberOfItemsInSection 被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆