集合视图中的部分数 [英] Number of sections in a collection view

查看:23
本文介绍了集合视图中的部分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个特定的 UIViewController 中有两个 UICollectionViews 并且我希望在它们两个中有不同数量的部分.我一直在以下列方式尝试这个

I have two UICollectionViews in a particular UIViewController and I wish to have a different number of sections in the two of them. I have been trying this in the following manner

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
if (collectionView == self.firstcollectionView) {
    return objects.count + 1;
   }
else
   {
    return 1;
   }
}

但由于某种原因它似乎不起作用.

But it does not seem to work for some reason.

也用标签尝试过,但也无法让它以这种方式工作.

Also tried it with tags but haven't been able to make it work that way either.

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
if (collectionView.tag == 1) {
    return objects.count + 1;
   }
else
   {
    return 1;
   }
}

当我能够检查是否有 1 个对象时,它总是在 UICollectionViews 中显示一个部分,这应该意味着第一个 UICollectionView 中将有 2 个部分代码>

It always shows a single section in both the UICollectionViews when I have been able to check that there is 1 object which should mean that there will be 2 sections in the first UICollectionView

更新 1:在调试时,似乎控件永远不会进入 if-case 总是返回 1.

UPDATE 1 : On debugging it seems the control never goes into the if-case returning 1 always.

更新 2:if 条件是问题所在.它永远不会满足.

UPDATE 2 : The if condition is where the problem is. It never gets satisfied.

更新 3:我能够通过将 UICollectionViews 声明为类变量而不是属性来使 if-else 工作.尽管如此,UICollectionViews 的部分数量仍然是 1.

UPDATE 3 : I was able to get the if-else working by declaring the UICollectionViews as a class variable rather than as a property. The number of sections is still 1 for both the UICollectionViews though.

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
if (collectionView == cardscollectionView) {
    return 2;
   }
else
   {
    return 1;
   }
}

还是不行.虽然这

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 2;
}

工作得很好.只是为了说明布局很好,两个单元格确实显示在两个集合视图中.

worked just fine. Just to illustrate the layouts are fine and 2 cells do show up in both the collection views.

样本项目(大量需求:D)-

SAMPLE PROJECT (On huge demand :D) -

https://github.com/genaks/UIICollectionView-Sections

我还没有达到重新加载 UICollectionView 的程度,因为当前设置还没有完全起作用

I have not yet gone to the extent of reloading the UICollectionView as the current setup hasn't been working altogether

推荐答案

确保您已将两个集合视图的委托和数据源设置为 self,例如

Make sure that you have set delegate and data source to self for both collection views like

   self.firstcollectionView.delegate = self;
   self.firstcollectionView.datasource = self;

第二个集合视图也一样.

And same for second collectionview also.

并确保您已正确连接插座.

And make sure that you have connected your outlet properly.

确保正确设置标签

更新:

 - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {

if (collectionView == self.cardscollectionView) {

    NSLog(@"getting called. It is cardcollerctionView");
    return 2;

}
if (collectionView == self.bankscollectionView) {
    NSLog(@"getting called. it is bankcollectionView");
    return 1;
}

else
{
    return 1;


}

    //use above or below both are working fine.
    //******************************* OR ********************************************

    if (collectionView == self.cardscollectionView) {

        NSLog(@"getting called. It is cardcollerctionView");
        return 2;

    }
    else
    {
        NSLog(@"it is else");
        return 1;


    }

 }

我已经运行了您的演示,并且运行良好.我用 numberofsection 方法更新了答案.代码的两个部分都在工作.确保您没有为两个 collectionViews

I have run your demo and it's work fine. I have updated answer with numberofsection method. both portion of code is working. Make sure that you are not using same UICollectionViewLayout for both collectionViews

希望这会有所帮助:)

这篇关于集合视图中的部分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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