UICollectionView insertItemsAtIndexPaths不重用单元格

时间:2013-10-26 11:55:31

标签: ios objective-c uicollectionview

将新单元格插入collectionviewinsertitemsatindexpaths不会deque任何单元格。每次运行此函数时,它会创建新的单元格,从而增加内存使用量我在循环中使用此方法插入大约400个项目,并且我在循环的每次迭代中插入一个单元格。或者换句话说,每次迭代都会调用一次。

是否有任何解决方案,因此它不会创建新的单元格?

if ([singletonObj.photosSortOrder isEqualToString:@"DESC"]) {
                [self.MNCollectionView insertItemsAtIndexPaths:@[[NSIndexPath   indexPathForItem:0 inSection:0]]];
            }else{
                [self.MNCollectionView insertItemsAtIndexPaths:@[[NSIndexPath   indexPathForItem:[photos count]-1 inSection:0]]];
            }

这被称为400次,每次调用它都会插入新单元而不是出列。还有其他人观察到这种现象吗?

UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

if ([cell.contentView.subviews count]>0) {
    [[cell.contentView.subviews objectAtIndex:0] removeFromSuperview];
}

// create the image views
UIImageView *MNPhotosImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 78, 78)];
[MNPhotosImageView setImage:[self.collectionPhotos objectAtIndex:[indexPath row]]];
[MNPhotosImageView setContentMode:UIViewContentModeScaleAspectFill];
[MNPhotosImageView setClipsToBounds:YES];

[cell setBackgroundView:MNPhotosImageView];

if (cell.selected==YES && isInEditMode==YES) {


    UIImageView *checkMarkView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 78, 78)];
    [checkMarkView setContentMode:UIViewContentModeScaleAspectFit];
    [checkMarkView setImage:[UIImage imageNamed:@"checkMark.png"]];

    [cell.contentView addSubview:checkMarkView];

}
//else{
//    [[cell.contentView.subviews objectAtIndex:0] removeFromSuperview];
//}

MNPhotosImageView=nil;

return cell;

0 个答案:

没有答案