Rui*_*res 53 xcode ios uicollectionview uicollectionviewcell
发生了什么
目前我有一个使用两个UICollectionViews内部的应用程序UITableView.这样我创建了一个类似应用程序的Pulse News.我的问题是,有时第6行和第11行完全消失,留下一个空白区域应该是单元格.我真的不介意,如果所有细胞都是这样的(这种方式我可以假设我没有做正确的事情),但问题是,正好发生在那些特定的细胞上.
我的理论
第6行和第11行是我开始滚动时出现的行,所以默认情况下我可以看到5个单元格,当我进行第一次水平滚动时,第6行会出现(有时会出现空白).
是)我有的
我目前唯一要做的就是:
[self.collectionView registerNib:[UINib nibWithNibName:CELL_NIB_NAME bundle:nil] forCellWithReuseIdentifier:CELL_IDENTIFIER];
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad.在创建细胞时:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
NSMutableDictionary *dictionary = [self.DataSource objectAtIndex:[indexPath row]];
[cell buildViewWithDictionary:dictionary withReferenceParent:self.referenceViewController];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
所以在我的承诺中没有任何幻想在这里发生.我虽然在数据源(虚拟JSON文件)上有问题,但有时它工作正常并且单元格显示,所以我猜从那部分是可以的.
所以我的"问题":有谁知道发生了什么?我真的不想说这是来自iOS的错误,但我想不出别的什么.
编辑1.0
令人惊讶的是这种方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)
在indexPath不计算[0,5]的情况下从[0,4]变为[0,6].我第一次真正在iOS中看到这种情况.
编辑2.0
我已经改变了创建单元格的方式,而不是出列我使用旧方法:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CELL_NIB_NAME owner:self options:nil];
MyCell *cell = (MyCell *)[nib objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)
仍然是同样悲伤的结果.
Rui*_*res 35
那么,做了什么工作?
1)子类UICollectionViewFlowLayout.
2)将my的flowLayout设置UICollectionView为我的新子类.
3)在UICollectionViewFlowLayout子类的init方法上,设置所需的方向:
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
Run Code Online (Sandbox Code Playgroud)
就我而言,它是水平的.
4)重要的部分:
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
在这一刻,我应该提出一点理论,但说实话,我没有任何线索.
Yao*_* Li 16
以上答案并没有为我工作,但下载图像后,我取代了代码
[self.myCollectionView reloadData]
与
[self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
刷新的CollectionView,它显示的所有单元格,你可以试试.
任何人给出的解决方案都没有帮助我在我们的应用程序中需要的自定义布局.相反,我必须这样做:(是的,它工作 !!!)
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
CGSize size = [self collectionViewContentSize];
rect.size.height = size.height*2;
NSArray *atrributes_Super = [super layoutAttributesForElementsInRect:rect];
return atrributes_Super;
}
Run Code Online (Sandbox Code Playgroud)
毕竟,UICollectionView只是寻找要在屏幕的rect中显示的元素的属性.