将新单元格插入collectionview
throughinsertitemsatindexpaths
不会影响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;