[英]UICollectionView Static CustomCell reuse
我在創建帶有自定義單元格的UICollectionView來顯示項目時遇到問題。 但是在刷新UICollectionView可重用單元后,填充了錯誤的索引
刷新之前的UICollectionView。
刷新后的UICollectionView。
可重用的收集視圖單元格的代碼示例。
- (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;
}
發生此問題的原因是單元將被重用。 單元被重用以改善系統性能。 如果您的表有1000個單元格,則系統不會分配1000個單元格,但是會比重用時少得多-
嘗試在if中添加else子句
if (indexPath.item != 0)
{
[cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]];
}
else
{
//Set your cell at index 0 with your camera image
[cell setCollectionItem:@"camera-image"];
}
認為它正在重用另一個單元格(在這種情況下為氣球),並且沒有為第一個索引單元格設置任何內容。 如果您使用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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.