In the previous article, the default cell selected for UICollectionView is that each cell has a corresponding identity, which means that the reuse mechanism of UICollectionView is abolished. For less data, it is OK, but for larger data, it will cause performance problems.
So I think that under the reuse mechanism of UICollectionView, the general idea of setting the default selected cell is to set a selectIndexPath to record when the cell is selected, and also use DeselectIndexPath to record when the cell is deselected. In addition to processing when the cell is selected and deselected, I also need to set when the cell is assigned data and cell is about to appear .
After setting the data for CollectionView, set cell 0 to be selected:
#pragma mark Set up collectionView Data - (void)setupCollectionViewData { for (int i = 0; i < 20; i++) { [self.dataArrayM addObject:[NSString stringWithFormat:@"The first%d individual 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]; }
Set the initial test value for seleceIndex in viewDidLoad, and in the method selected in collectionView, assign the following value:
- (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]; // Set up collectionView Data [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]]; }
In the agent method unchecked by collectionView, assign a value to DeselectIndexPath:
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { self.DeselectIndexpath = indexPath; LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; if (cell == nil) { // If you can't get it after reuse cell,Go straight back return; } [cell setBackgroundColor:[UIColor grayColor]]; [cell.nameLabel setTextColor:[UIColor blackColor]]; }
In the data source method of cell assignment, set the selected style of 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; }
Set the selected and unselected styles in the proxy method shown in the 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]]; } }
The complete code is as follows:
// // ViewController.m // testSelect // // Created by Li Jiang Bo on 2019/4/22. // Copyright © 2019 year jinxiaofu. All rights reserved. // #import "ViewController.h" #import "LBCollectionViewCell.h" static NSString *const cellId = @"cellId"; @interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> // Data array @property (nonatomic, strong) NSMutableArray *dataArrayM; @property (nonatomic, weak) UICollectionView *testCollectionView; // Selection cell Of indexPath @property (nonatomic, strong) NSIndexPath *selectIndexPath; // Unchecked cell,Prevent not set in unchecked proxy method due to reuse @property (nonatomic, strong) NSIndexPath *DeselectIndexpath; @end @implementation ViewController - (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]; // Set up collectionView Data [self setupCollectionViewData]; } #pragma mark - private Method #pragma mark Set up collectionView Data - (void)setupCollectionViewData { for (int i = 0; i < 20; i++) { [self.dataArrayM addObject:[NSString stringWithFormat:@"The first%d individual 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]; } #pragma mark - setupUI #pragma mark setupUICollectionView - (void)setupUICollectionView { // Set up uicollectionView style UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.minimumLineSpacing = 15; flowLayout.minimumInteritemSpacing = 15; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; UICollectionView *testCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout]; [testCollectionView registerClass:[LBCollectionViewCell class] forCellWithReuseIdentifier:cellId]; testCollectionView.delegate = self; testCollectionView.dataSource = self; [testCollectionView setBackgroundColor:[UIColor whiteColor]]; [self.view addSubview:testCollectionView]; self.testCollectionView = testCollectionView; } #pragma mark - UICollectionViewDatasource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [self.dataArrayM count]; } - (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; } #pragma mark - UICollectionViewDelegate - (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(150, 150); } - (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]]; } - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { self.DeselectIndexpath = indexPath; LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; if (cell == nil) { // If you can't get it after reuse cell,Go straight back return; } [cell setBackgroundColor:[UIColor grayColor]]; [cell.nameLabel setTextColor:[UIColor blackColor]]; } - (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]]; } } #pragma mark - Lazy loading - (NSMutableArray *)dataArrayM { if (!_dataArrayM) { _dataArrayM = [NSMutableArray array]; } return _dataArrayM; } @end
github address: https://github.com/OLeGeB/selectCollectionViewCell.git