cellForItemAtIndexPath not called but numberOfItem

2019-02-02 19:53发布

This could be a duplicate post but I haven't found the solution in any of this:

enter image description here

I'm setting my UICollectionView as:

UINib *nib = [UINib nibWithNibName:@"CollectionViewCell"
                           bundle:[NSBundle mainBundle]];
[_collectionView registerNib: nib forCellWithReuseIdentifier:@"bCell"];

(I've tried to set delegate, without delegate, etcetera).

But then, only the

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section gets called while

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath doesn't.

I've checked the numberOfItemsInSection I return, and it's always > 0 (it's exactly 17).

I've checked to call [_collectionView reloadData]; and nothing happens either.

I've tried different things but I can't make it work.

Furthermore, my UICollectionView should be here but it's not:

enter image description here

Does anyone have a clue? Thank you.

12条回答
Deceive 欺骗
2楼-- · 2019-02-02 20:09

I just found that NONE of cellForItemAtIndexPath gets called (in iOS 10) as long as at least ONE cell has a height of zero.

Disclaimer: sorry if this should have been a comment - not enough reputation.

查看更多
够拽才男人
3楼-- · 2019-02-02 20:11

I had same problem with collection view outlet on a storyboard with "standard" constraints (bottom, top, leading, trailing fixed to inner view).

I solved in this way:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = self.collectionView.frame.size
查看更多
Emotional °昔
4楼-- · 2019-02-02 20:12

1. cellForItemAtIndexPath will be called when you return More than 0 Items on numberOfItemsInSection method.

2. Check that your UICollectionViewFlowLayout is correct or your own. UICollectionViewLayout is abstract class you can't use it directly

3. Check the size of the collection view cell & Collection view.

4. Try to set your collectionview size by in code. [yourCollectionViewFlowLayout setItemSize:CGSizeMake(200, 200)];

5.If any of them NOT works, then kill the XCode & restart . Sometimes it will fix the issue.

Apple says

The UICollectionViewLayout class is an abstract base class that you subclass and use to generate layout information for a collection view. The job of a layout object is to determine the placement of cells, supplementary views, and decoration views inside the collection view’s bounds and to report that information to the collection view when asked. The collection view then applies the provided layout information to the corresponding views so that they can be presented onscreen.

You must subclass UICollectionViewLayout in order to use it. Before you consider subclassing, though, you should look at the UICollectionViewFlowLayout class to see if it can be adapted to your layout needs.

查看更多
▲ chillily
5楼-- · 2019-02-02 20:12

For me the cellForItemAtIndexPath not called for the first time after i set the constraint of the collectionView to 0 and then again setting it to its Actual Size.

 @IBOutlet weak var collectionVwHeightContraint: NSLayoutConstraint!
 if attachments.count > 0 {

     collectionVwHeightContraint.constant = 100
 }else {

     collectionVwHeightContraint.constant = 0
 }
 // collectionVwHeightContraint is my collectionView height Contraint outlet

I solved this by calling:

self.view.layoutIfNeeded()

Hope it will help someone :)

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-02-02 20:12

For me it was that my collectionView's container didn't have translatesAutoresizingMaskIntoConstraints = false

查看更多
ら.Afraid
7楼-- · 2019-02-02 20:18

Ok I think this could be useful for future problems with this...

It was because of constraints on the StoryBoard. I don't really understand why that happens, because I just had fixed an horizontal space. I removed the horizontal space as constraint for that View and everything works fine.

Well, let's life flow.

查看更多
登录 后发表回答