发生了什么
目前我有一个使用两个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)
仍然是同样悲伤的结果.
上下文:我使用的UICollectionView是一个photoview.每张照片都是一张细胞UIImage.图像可以有不同的大小,我希望它们填满整个屏幕.所以我写了一个类来确定每个单元的框架,UICollectionCell让一个子UICollectionViewFlowLayout类为每个项目请求该框架为正确的框架.
我的执行情况 UICollectionViewFlowLayoutClass:
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
let attributesToReturn = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes]
for attributes in attributesToReturn ?? [] {
if attributes.representedElementCategory == .Cell {
let frame = self.layoutAttributesForItemAtIndexPath(attributes.indexPath).frame
attributes.size = frame.size
attributes.frame = frame
}
}
return attributesToReturn
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
let curAttributes = super.layoutAttributesForItemAtIndexPath(indexPath)
let frame = mazeManager.sizeForItemAtIndex(indexPath, collectionView: collectionView!)
curAttributes.size = frame.size
curAttributes.frame = frame
return curAttributes
}
Run Code Online (Sandbox Code Playgroud)
所以框架要求我的MazeManager给出一个框架.返回的帧似乎是正确的,它们都适合UICollectionView.
当我打开我的应用程序时,一切看起来都很好,即使我滚动.但是当我滚动到一个特定的位置时(这个位置感觉随机,因为它取决于我测试的图像,但是使用相同的图像集,位置是相同的)细胞从我的视图中消失.当我向后滚动它们返回.
我已经检查过哪些细胞没有被隐藏,但它们从未被隐藏过. …