UICollectionView的重用问题
1、在Cell类型不同的时候,需要事先注册不同的identifier才能在
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
NSString *identifier = [NSString stringWithFormat:[NSString stringWithFormat:@"main%d",indexPath.row]];
MainViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
中使用。
2、Cell类型相同的时候,在Cell里一定要调用 prepareForReuse方法来保持重用的cell的清洁。
具体方法就是把需要赋值的对象=nil即可。但是注意在方法内调用[super prepareForReuse];
还有一点。很急,很关键。
偶然发现了prepareForReuse方法只要cell滑出Collection的显示范围就会执行这个方法,那就为了解决重用问题,可以小小的trick一下。
在注册cell类的时候,
for (int i = 0; i<_dataArray.count; i++) {
[_mainCollectionView registerClass:[MainViewCollectionViewCell class]forCellWithReuseIdentifier:[NSString stringWithFormat:@"main%d",i]];
}
还有一点。很急,很关键。
偶然发现了prepareForReuse方法只要cell滑出Collection的显示范围就会执行这个方法,那就为了解决重用问题,可以小小的trick一下。
在注册cell类的时候,
for (int i = 0; i<_dataArray.count; i++) {
[_mainCollectionView registerClass:[MainViewCollectionViewCell class]forCellWithReuseIdentifier:[NSString stringWithFormat:@"main%d",i]];
}
在给cell赋值的方法中,使用:
NSString *identifier = [NSString stringWithFormat:[NSString stringWithFormat:@"main%d",indexPath.row]];
MainViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
轻松&愉快的完成了“准”重用过程~
评论
发表评论