iOS开发之 collectionView 刷新单个重影问题
2021-06-16 本文已影响0人
安静SRR
在做项目使用collectionView的时候遇到了只需刷新某个cell的情况。按照常规思路去刷新:
NSIndexPath *topHeaderViewIndexpath = [[self.tableView indexPathsForVisibleRows] firstObject];
NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];
if (![moveToIndexpath isEqual:self.selectIndex] && self.isclickCollection != YES) {
NSIndexPath *lastIndex = self.selectIndex;
self.selectIndex = moveToIndexpath;
[self.topCollectionView reloadItemsAtIndexPaths:@[lastIndex,moveToIndexpath]];
[self.topCollectionView selectItemAtIndexPath:moveToIndexpath animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
}
然后发现刷新的时候会有重影,后来改成如下代码成功解决重影问题:
NSIndexPath *topHeaderViewIndexpath = [[self.tableView indexPathsForVisibleRows] firstObject];
NSIndexPath *moveToIndexpath = [NSIndexPath indexPathForRow:topHeaderViewIndexpath.section inSection:0];
if (![moveToIndexpath isEqual:self.selectIndex] && self.isclickCollection != YES) {
NSIndexPath *lastIndex = self.selectIndex;
self.selectIndex = moveToIndexpath;
//解决重影重点
[UIView performWithoutAnimation:^{
[self.topCollectionView reloadItemsAtIndexPaths:@[lastIndex,moveToIndexpath]];
[self.topCollectionView selectItemAtIndexPath:moveToIndexpath animated:YES scrollPosition:UICollectionViewScrollPositionLeft];
}];
}