上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制。对于较少的数据情况是可以的,但是对于数据比较大,就会造成性能问题。
于是思考在UICollectionView重用机制下,设置默认选中的cell,大致思路就是在cell被选中的时候设置一个selectIndexPath记录下来,在cell被取消选中的时候也用DeselectIndexPath记录下来,除了在cell被选中和取消选中的时候处理,还要在cell被赋值数据和cell即将出现的时候设置。
在为CollectionView设置完数据之后,设置第0个cell被选中:
- #pragma mark 设置collectionView的数据
- - (void)setupCollectionViewData {
-
- for (int i = 0; i < 20; i++) {
- [self.dataArrayM addObject:[NSString stringWithFormat:@"第%d个cell",i]];
- }
-
- [self.testCollectionView reloadData];
-
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
-
- [self.testCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
- [self collectionView:self.testCollectionView didSelectItemAtIndexPath:indexPath];
- }
在viewDidLoad中为seleceIndex设置初试值,并在collectionView选中的方法中,赋值:
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
-
- [self setupUICollectionView];
-
- // 设置collectionView的数据
- [self setupCollectionViewData];
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
-
- self.selectIndexPath = indexPath;
- LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
- [cell setBackgroundColor:[UIColor greenColor]];
- [cell.nameLabel setTextColor:[UIColor redColor]];
- }
在collectionView取消选中的代理方法中,为DeselectIndexPath赋值:
- - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
- self.DeselectIndexpath = indexPath;
- LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
- if (cell == nil) { // 如果重用之后拿不到cell,就直接返回
- return;
- }
- [cell setBackgroundColor:[UIColor grayColor]];
- [cell.nameLabel setTextColor:[UIColor blackColor]];
- }
在cell赋值的数据源方法中,设置cell的选中的样式:
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- LBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
- [cell.nameLabel setText:self.dataArrayM[indexPath.row]];
-
- if ([self.selectIndexPath isEqual:indexPath]) {
- [cell setBackgroundColor:[UIColor greenColor]];
- [cell.nameLabel setTextColor:[UIColor redColor]];
- } else {
- [cell setBackgroundColor:[UIColor grayColor]];
- [cell.nameLabel setTextColor:[UIColor blackColor]];
- }
-
-
-
- return cell;
- }
在cell出现正在展示的代理方法中再设置选中和未选中的样式:
- - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
- LBCollectionViewCell *LBcell = (LBCollectionViewCell *)cell;
- if (self.DeselectIndexpath && [self.DeselectIndexpath isEqual:indexPath]) {
-
- [LBcell setBackgroundColor:[UIColor grayColor]];
- [LBcell.nameLabel setTextColor:[UIColor blackColor]];
- }
-
- if ([self.selectIndexPath isEqual:indexPath]) {
- [LBcell setBackgroundColor:[UIColor greenColor]];
- [LBcell.nameLabel setTextColor:[UIColor redColor]];
- }
- }
完整代码如下:
github地址: https://github.com/OLeGeB/selectCollectionViewCell.git