集合视图中的部分数量

我有两个UICollectionViews在一个特定的UIViewController ,我希望在他们两个有不同数量的部分。 我一直在以下列方式尝试

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

但是由于某种原因,这似乎不起作用。

也尝试过与标签,但还没有能够使它的工作方式。

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

它总是在两个UICollectionViews显示一个单独的部分,当我能够检查有一个对象,这意味着在第一个UICollectionView会有两个部分

更新1:在debugging看来,控制永远不会进入如果总是返回1。

更新2:如果条件是问题的地方。 它从来没有得到满足。

更新3:我能够通过将UICollectionViews声明为类variables而不是属性来获得if-else的工作。 虽然UICollectionViews的部分数仍然是1。

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

仍然没有工作。 虽然这样

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

工作得很好。 只是为了说明布局是好的,2个单元显示在两个收集视图。

示例项目(在巨大的需求:D) –

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

我还没有去重新加载UICollectionView作为目前的设置尚未完全工作

确保你已经设置委托和数据源为自己的收集视图像

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

第二个collectionview也一样。

并确保你已经正确连接你的sockets。

要么

确保您正确设置标签

更新:

  - (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都不使用相同的UICollectionViewLayout

希望这会帮助:)

我想你不会正确设置标签,首先在viewDidLoad:方法中设置标签,例如下面的例子,

 - (void)viewDidLoad { [super viewDidLoad]; // put your code... firstCollectionView.tag = 10000; secondCollectioView.tag = 9000; } 

或者在故事板中设置标签在下面,

在这里输入图像说明

那么你会检查的条件是

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

希望它有帮助

这一定会工作。 尝试一下。

  -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView { if([collectionView isEqual:self.firstcollectionView]){ //return object count for firstCollectionView; } else if([collectionView isEqual:self.secondCollectionView]) { //return object count for secondCollectionView; } } 

您的初始方法检查UICollectionView的身份没有标签确实工作,并比使用标签更强大,但如果对象计数是0,那么部分计数将仍然是相同的。 你提到的计数是从networking元素加载后,最初是0,然后是1。 你是否在web加载完成后在collectionViews上调用reloadData()

下面是在代码中创build两个UICollectionView的完整工作示例(不带任何Web抓取),可以使用不同的节数:

 class CustomCell: UICollectionViewCell {} class DoubleViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { var firstCollectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() let v = UICollectionView(frame: CGRectZero, collectionViewLayout: layout) v.translatesAutoresizingMaskIntoConstraints = false v.registerClass(CustomCell.self, forCellWithReuseIdentifier: "CustomCell") return v }() var secondCollectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() let v = UICollectionView(frame: CGRectZero, collectionViewLayout: layout) v.translatesAutoresizingMaskIntoConstraints = false v.registerClass(CustomCell.self, forCellWithReuseIdentifier: "CustomCell") return v }() override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(self.firstCollectionView) self.view.addSubview(self.secondCollectionView) self.firstCollectionView.delegate = self self.secondCollectionView.delegate = self self.firstCollectionView.dataSource = self self.secondCollectionView.dataSource = self } func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { if collectionView == self.firstCollectionView { return 1 } return 2 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 5 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell cell.backgroundColor = randomColor() return cell } override func updateViewConstraints() { super.updateViewConstraints() self.firstCollectionView.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true self.firstCollectionView.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true self.firstCollectionView.bottomAnchor.constraintEqualToAnchor(self.secondCollectionView.topAnchor).active = true self.firstCollectionView.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor).active = true self.secondCollectionView.bottomAnchor.constraintEqualToAnchor(self.bottomLayoutGuide.topAnchor).active = true self.secondCollectionView.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true self.secondCollectionView.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true self.firstCollectionView.heightAnchor.constraintEqualToAnchor(self.secondCollectionView.heightAnchor).active = true } } 

由于不使用xibs / storyboard,因此需要额外的约束代码

尝试这个:

 - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { if([collectionView isEqual: collectionView1]) { return CollectionArray1.count; } else if ([collectionView isEqual: collectionView2]) { return CollectionArray2.count; } else { NSLog(@"Dummy Text"); } return 1; } 

也许,在执行numberOfSectionsInCollectionView之前,您的cardscollectionView已经更改,或者您以错误的方式设置了委托。

添加下面的代码来检查两个collectionView的委托是否是self

正确的日志是两个不同的 collectionView地址,并且只有一个 cardscollectionView地址。

 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { NSLog(@"collectionView=%@", collectionView); NSLog(@"cardscollectionView=%@", cardscollectionView); if (collectionView == cardscollectionView) { return 2; } else{ return 1; } } 

问题是我使用相同的UICollectionViewLayout两个UICollectionViews。 使用2个不同的UICollectionViewLayouts解决了我的问题。 感谢大家的帮助。 我正准备为此疯狂。