经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
UICollectionView设置首个cell默认选中(二)
来源:cnblogs  作者:木子沉雨  时间:2019/4/23 8:47:02  对本文有异议

上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制。对于较少的数据情况是可以的,但是对于数据比较大,就会造成性能问题。

于是思考在UICollectionView重用机制下,设置默认选中的cell,大致思路就是在cell被选中的时候设置一个selectIndexPath记录下来,在cell被取消选中的时候也用DeselectIndexPath记录下来,除了在cell被选中和取消选中的时候处理,还要在cell被赋值数据和cell即将出现的时候设置。

在为CollectionView设置完数据之后,设置第0个cell被选中:

  1. #pragma mark 设置collectionView的数据
  2. - (void)setupCollectionViewData {
  3. for (int i = 0; i < 20; i++) {
  4. [self.dataArrayM addObject:[NSString stringWithFormat:@"第%d个cell",i]];
  5. }
  6. [self.testCollectionView reloadData];
  7. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  8. [self.testCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  9. [self collectionView:self.testCollectionView didSelectItemAtIndexPath:indexPath];
  10. }

在viewDidLoad中为seleceIndex设置初试值,并在collectionView选中的方法中,赋值:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view, typically from a nib.
  4. self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  5. [self setupUICollectionView];
  6. // 设置collectionView的数据
  7. [self setupCollectionViewData];
  8. }
  1. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  2. self.selectIndexPath = indexPath;
  3. LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  4. [cell setBackgroundColor:[UIColor greenColor]];
  5. [cell.nameLabel setTextColor:[UIColor redColor]];
  6. }

在collectionView取消选中的代理方法中,为DeselectIndexPath赋值:

  1. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
  2. self.DeselectIndexpath = indexPath;
  3. LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  4. if (cell == nil) { // 如果重用之后拿不到cell,就直接返回
  5. return;
  6. }
  7. [cell setBackgroundColor:[UIColor grayColor]];
  8. [cell.nameLabel setTextColor:[UIColor blackColor]];
  9. }

在cell赋值的数据源方法中,设置cell的选中的样式:

  1. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  2. LBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
  3. [cell.nameLabel setText:self.dataArrayM[indexPath.row]];
  4. if ([self.selectIndexPath isEqual:indexPath]) {
  5. [cell setBackgroundColor:[UIColor greenColor]];
  6. [cell.nameLabel setTextColor:[UIColor redColor]];
  7. } else {
  8. [cell setBackgroundColor:[UIColor grayColor]];
  9. [cell.nameLabel setTextColor:[UIColor blackColor]];
  10. }
  11. return cell;
  12. }

在cell出现正在展示的代理方法中再设置选中和未选中的样式:

  1. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  2. LBCollectionViewCell *LBcell = (LBCollectionViewCell *)cell;
  3. if (self.DeselectIndexpath && [self.DeselectIndexpath isEqual:indexPath]) {
  4. [LBcell setBackgroundColor:[UIColor grayColor]];
  5. [LBcell.nameLabel setTextColor:[UIColor blackColor]];
  6. }
  7. if ([self.selectIndexPath isEqual:indexPath]) {
  8. [LBcell setBackgroundColor:[UIColor greenColor]];
  9. [LBcell.nameLabel setTextColor:[UIColor redColor]];
  10. }
  11. }

完整代码如下:

  1. //
  2. // ViewController.m
  3. // testSelect
  4. //
  5. // Created by 李江波 on 2019/4/22.
  6. // Copyright © 2019年 jinxiaofu. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "LBCollectionViewCell.h"
  11.  
  12.  
  13. static NSString *const cellId = @"cellId";
  14. @interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
  15. // 数据数组
  16. @property (nonatomic, strong) NSMutableArray *dataArrayM;
  17. @property (nonatomic, weak) UICollectionView *testCollectionView;
  18. // 选中cell的indexPath
  19. @property (nonatomic, strong) NSIndexPath *selectIndexPath;
  20. // 取消选中的cell,防止由于重用,在取消选中的代理方法中没有设置
  21. @property (nonatomic, strong) NSIndexPath *DeselectIndexpath;
  22. @end
  23.  
  24. @implementation ViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. // Do any additional setup after loading the view, typically from a nib.
  28. self.selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  29. [self setupUICollectionView];
  30. // 设置collectionView的数据
  31. [self setupCollectionViewData];
  32. }
  33. #pragma mark - private Method
  34. #pragma mark 设置collectionView的数据
  35. - (void)setupCollectionViewData {
  36. for (int i = 0; i < 20; i++) {
  37. [self.dataArrayM addObject:[NSString stringWithFormat:@"第%d个cell",i]];
  38. }
  39. [self.testCollectionView reloadData];
  40. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  41. [self.testCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  42. [self collectionView:self.testCollectionView didSelectItemAtIndexPath:indexPath];
  43. }
  44. #pragma mark - setupUI
  45. #pragma mark setupUICollectionView
  46. - (void)setupUICollectionView {
  47. // 设置uicollectionView样式
  48. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  49. flowLayout.minimumLineSpacing = 15;
  50. flowLayout.minimumInteritemSpacing = 15;
  51. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  52. UICollectionView *testCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
  53. [testCollectionView registerClass:[LBCollectionViewCell class] forCellWithReuseIdentifier:cellId];
  54. testCollectionView.delegate = self;
  55. testCollectionView.dataSource = self;
  56. [testCollectionView setBackgroundColor:[UIColor whiteColor]];
  57. [self.view addSubview:testCollectionView];
  58. self.testCollectionView = testCollectionView;
  59. }
  60. #pragma mark - UICollectionViewDatasource
  61. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  62. return 1;
  63. }
  64. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  65. return [self.dataArrayM count];
  66. }
  67. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  68. LBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
  69. [cell.nameLabel setText:self.dataArrayM[indexPath.row]];
  70. if ([self.selectIndexPath isEqual:indexPath]) {
  71. [cell setBackgroundColor:[UIColor greenColor]];
  72. [cell.nameLabel setTextColor:[UIColor redColor]];
  73. } else {
  74. [cell setBackgroundColor:[UIColor grayColor]];
  75. [cell.nameLabel setTextColor:[UIColor blackColor]];
  76. }
  77. return cell;
  78. }
  79. #pragma mark - UICollectionViewDelegate
  80. - (CGSize) collectionView:(UICollectionView *)collectionView
  81. layout:(UICollectionViewLayout *)collectionViewLayout
  82. sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. return CGSizeMake(150, 150);
  85. }
  86. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  87. self.selectIndexPath = indexPath;
  88. LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  89. [cell setBackgroundColor:[UIColor greenColor]];
  90. [cell.nameLabel setTextColor:[UIColor redColor]];
  91. }
  92. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
  93. self.DeselectIndexpath = indexPath;
  94. LBCollectionViewCell *cell = (LBCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
  95. if (cell == nil) { // 如果重用之后拿不到cell,就直接返回
  96. return;
  97. }
  98. [cell setBackgroundColor:[UIColor grayColor]];
  99. [cell.nameLabel setTextColor:[UIColor blackColor]];
  100. }
  101. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  102. LBCollectionViewCell *LBcell = (LBCollectionViewCell *)cell;
  103. if (self.DeselectIndexpath && [self.DeselectIndexpath isEqual:indexPath]) {
  104. [LBcell setBackgroundColor:[UIColor grayColor]];
  105. [LBcell.nameLabel setTextColor:[UIColor blackColor]];
  106. }
  107. if ([self.selectIndexPath isEqual:indexPath]) {
  108. [LBcell setBackgroundColor:[UIColor greenColor]];
  109. [LBcell.nameLabel setTextColor:[UIColor redColor]];
  110. }
  111. }
  112. #pragma mark - 懒加载
  113. - (NSMutableArray *)dataArrayM {
  114. if (!_dataArrayM) {
  115. _dataArrayM = [NSMutableArray array];
  116. }
  117. return _dataArrayM;
  118. }
  119. @end

 github地址: https://github.com/OLeGeB/selectCollectionViewCell.git

原文链接:http://www.cnblogs.com/muzichenyu/p/10750736.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号