ios cellForItemAtIndexPath 未调用但 numberOfItemsInSection 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30804937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cellForItemAtIndexPath not called but numberOfItemsInSection does
提问by Rafael Ruiz Mu?oz
This could be a duplicate post but I haven't found the solution in any of this:
这可能是一个重复的帖子,但我还没有找到解决方案:
I'm setting my UICollectionView
as:
我将我的设置UICollectionView
为:
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
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
被调用时
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
doesn't.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
没有。
I've checked the numberOfItemsInSectionI return, and it's always > 0 (it's exactly 17).
我检查了我返回的numberOfItemsInSection,它总是 > 0(正好是 17)。
I've checked to call [_collectionView reloadData];
and nothing happens either.
我检查了打电话[_collectionView reloadData];
,也没有任何反应。
I've tried different things but I can't make it work.
我尝试了不同的东西,但我不能让它工作。
Furthermore, my UICollectionView
should be here but it's not:
此外,我UICollectionView
应该在这里,但它不是:
Does anyone have a clue? Thank you.
有人有线索吗?谢谢你。
采纳答案by Rafael Ruiz Mu?oz
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.
这是因为 StoryBoard 的限制。我真的不明白为什么会这样,因为我刚刚固定了一个水平空间。我删除了作为该视图约束的水平空间,一切正常。
Well, let's life flow.
好吧,让我们的生活流动起来。
回答by Narayana
I also face same issue and finally got to know that i have passed wrong class object for collectionViewLayout , i change to UICollectionViewFlowLayoutinstead of UICollectionViewLayoutwhile creating object for UICollectionView like below its stared calling all delegate method.
我也面临同样的问题,终于知道我为 collectionViewLayout 传递了错误的类对象,我在为UICollectionView创建对象时更改为UICollectionViewFlowLayout而不是UICollectionViewLayout 如下所示,它盯着调用所有委托方法。
collectionView = UICollectionView(frame: CGRectMake(5, 0, 400, 40) , collectionViewLayout: UICollectionViewFlowLayout())
回答by Vijay-Apple-Dev.blogspot.com
1. cellForItemAtIndexPath will be called when you return More than 0 Items on numberOfItemsInSection method.
1. cellForItemAtIndexPath 当您在 numberOfItemsInSection 方法上返回多于 0 个项目时将被调用。
2. Check that your UICollectionViewFlowLayout is correct or your own. UICollectionViewLayoutis abstract class you can't use it directly
2. 检查您的 UICollectionViewFlowLayout 是正确的还是您自己的。UICollectionViewLayout是抽象类,不能直接使用
3. Check the size of the collection view cell & Collection view.
3. 检查集合视图单元格和集合视图的大小。
4. Try to set your collectionview size by in code.[yourCollectionViewFlowLayout setItemSize:CGSizeMake(200, 200)];
4. 尝试在代码中设置您的 collectionview 大小。[yourCollectionViewFlowLayout setItemSize:CGSizeMake(200, 200)];
5.If any of them NOT works, then kill the XCode & restart . Sometimes it will fix the issue.
5.如果其中任何一个不起作用,则杀死 XCode 并重新启动。有时它会解决问题。
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.
UICollectionViewLayout 类是一个抽象基类,您可以将其子类化并用于为集合视图生成布局信息。布局对象的工作是确定单元格、补充视图和装饰视图在集合视图边界内的位置,并在被询问时将该信息报告给集合视图。然后,集合视图将提供的布局信息应用于相应的视图,以便它们可以显示在屏幕上。
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.
您必须继承 UICollectionViewLayout 才能使用它。不过,在考虑子类化之前,您应该查看 UICollectionViewFlowLayout 类,看看它是否可以适应您的布局需求。
回答by reza_khalafi
Add this to viewDidLoad:
将此添加到 viewDidLoad:
self.automaticallyAdjustsScrollViewInsets = NO;
回答by frank
I meet this problem too. I checked all the possible issues, everything looks ok. Then I tried to changed the reload methods.
我也遇到这个问题。我检查了所有可能的问题,一切正常。然后我尝试更改重新加载方法。
[self.colView reloadData]
-> [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:x]];
[self.colView reloadData]
-> [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:x]];
Then all things worked. However I still don't understand how.
然后一切都奏效了。但是我仍然不明白如何。
回答by KiRviz
I just found that NONE of cellForItemAtIndexPath gets called (in iOS 10) as long as at least ONE cell has a height of zero.
我刚刚发现,只要至少一个单元格的高度为零,就不会调用 cellForItemAtIndexPath 的 NONE(在 iOS 10 中)。
Disclaimer: sorry if this should have been a comment - not enough reputation.
免责声明:对不起,如果这应该是评论 - 没有足够的声誉。
回答by Ariven Nadar
For me the cellForItemAtIndexPathnot called for the first time after i set the constraint of the collectionView to 0 and then again setting it to its Actual Size.
对我来说,在我将 collectionView 的约束设置为 0 然后再次将其设置为其实际大小后,第一次没有调用cellForItemAtIndexPath。
@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 :)
希望它会帮助某人:)
回答by WingJammer
Might help somebody else, it could be 2 things:
可能会帮助别人,这可能是两件事:
- Height of the cell is not calculated properly, so need to check the constraints of the cell.
- 单元格的高度计算不正确,因此需要检查单元格的约束。
To fix this, I implemented UICollectionViewLayout's method: extension TableViewCellActionItemPageControl: UICollectionViewDelegateFlowLayout {
为了解决这个问题,我实现了 UICollectionViewLayout 的方法:extension TableViewCellActionItemPageControl: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 414, height: 213)
}
- Other possibility is: the size of the collection view cell returned is more that the collection view itself. Make sure the height of your cell is smaller than the height of your collective.
- 另一种可能性是:返回的集合视图单元格的大小大于集合视图本身。确保您的单元格的高度小于您的集体的高度。
回答by Luiz Dias
What helped me was to set the correct itemSize
. My cellForItemAtIndexPath
was not being called because of the wrong height.
帮助我的是设置正确的itemSize
. cellForItemAtIndexPath
由于高度错误,我没有被调用。
So I set the size at:
所以我将大小设置为:
[self.myCollectionViewFlowLayout setItemSize:CGSizeMake(170, 300)];
回答by Liron Yahdav
For me the issue was that I was creating my Collection View programmatically but I had given it CGRectZero
as its frame. I was laying it out using Auto Layout after viewDidLoad
. If I lay out the collection view while the view controller is loading, it ends up calling cellForItemAtIndexPath:
correctly.
对我来说,问题是我正在以编程方式创建我的集合视图,但我已将其CGRectZero
作为框架。我在viewDidLoad
. 如果我在加载视图控制器时布置集合视图,它最终会cellForItemAtIndexPath:
正确调用。