我在使用自定义单元格创建 UICollectionView 以显示项目时遇到问题。但是在 UICollectionView 刷新后,可重用的单元格填充了错误的索引
刷新前的 UICollectionView。
刷新后的 UICollectionView。
可重用 Collection View 单元格的代码示例。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (indexPath.item != 0)
{
[cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]];
}
return cell;
}
最佳答案
认为它正在重用另一个单元格(在本例中为气球)并且没有为第一个索引单元格设置任何内容。如果您使用 else 语句来创建新的相机单元格,希望它会重新出现。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (indexPath.item != 0) {
[cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]];
} else {
// Set camera item here
}
return cell;
}
关于ios - UICollectionView 静态 CustomCell 复用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29434799/