UICollectionView设置首个cell默认选中(二)
程序员文章站
2024-02-01 17:57:46
上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制。对于较少的数据情况是可以的,但是对于数据比较大,就会造成性能问题。 于是思考在UICollectionView重用机制下,设置默认选中的cell, ......
上篇对于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]]; } }
完整代码如下:
// // viewcontroller.m // testselect // // created by 李江波 on 2019/4/22. // copyright © 2019年 jinxiaofu. all rights reserved. // #import "viewcontroller.h" #import "lbcollectionviewcell.h" static nsstring *const cellid = @"cellid"; @interface viewcontroller ()<uicollectionviewdelegate, uicollectionviewdatasource> // 数据数组 @property (nonatomic, strong) nsmutablearray *dataarraym; @property (nonatomic, weak) uicollectionview *testcollectionview; // 选中cell的indexpath @property (nonatomic, strong) nsindexpath *selectindexpath; // 取消选中的cell,防止由于重用,在取消选中的代理方法中没有设置 @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]; // 设置collectionview的数据 [self setupcollectionviewdata]; } #pragma mark - private method #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]; } #pragma mark - setupui #pragma mark setupuicollectionview - (void)setupuicollectionview { // 设置uicollectionview样式 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) { // 如果重用之后拿不到cell,就直接返回 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 - 懒加载 - (nsmutablearray *)dataarraym { if (!_dataarraym) { _dataarraym = [nsmutablearray array]; } return _dataarraym; } @end
github地址: https://github.com/olegeb/selectcollectionviewcell.git
上一篇: 异步加载树节点
下一篇: 科学计算库Numpy——数组形状