我正在将标准 UICollectionView 与自定义单元格连接起来。
长话短说,当我重新加载 collectionView 时,单元格不会按照它们创建的顺序出列。
前任。indexPath.row 5 的单元格返回 indexPath.row 0。
当我重新加载 collectionView 时,这会导致闪烁。我想在重新加载 collectionView 时重用同一个单元格,但如果数据相同,则不想重新加载数据。因为它是错误的顺序,它永远不会相同。
有没有其他人对此感到困扰或有解决办法?UITableView 不这样做,它以相同的顺序使用单元格,为什么 collectionView 不这样做?
- (id)initWithUser:(User *)user andCollectionView:(UICollectionView *)collectionView {
if (self = [super initWithCollectionView:collectionView]) {
_collectionView = collectionView;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.showsVerticalScrollIndicator = YES;
_collectionView.pagingEnabled = NO;
_collectionView.bounces = YES;
_collectionView.alwaysBounceVertical = YES;
[_collectionView registerClass:[SubmissionMinimalCollectionViewCell class]
forCellWithReuseIdentifier:SubmissionCollectionCellIdentifier];
UIEdgeInsets scrollInsets = self.collectionView.scrollIndicatorInsets;
scrollInsets.right = 1;
_collectionView.scrollIndicatorInsets = scrollInsets;
__weak typeof(self) weakSelf = self;
self.data = [self.user fetchSubmissionsByPage:self.pageCount
objectsPerPage:self.perPage
andForceRefresh:YES
withCompletion:^(NSArray *submissions) {
weakSelf.pageCount++;
weakSelf.data = submissions;
[weakSelf.collectionView reloadData];
}];
[self.collectionView reloadData];
}
return self;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SubmissionMinimalCollectionViewCell *cell = [self.collectionView
dequeueReusableCellWithReuseIdentifier:SubmissionCollectionCellIdentifier
forIndexPath:indexPath];
[cell prepareForUseWithSubmission:[self.data objectAtIndex:indexPath.row]];
cell.row = indexPath.row;
return cell;
}