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 = ; i < ; i++) {
[self.dataArrayM addObject:[NSString stringWithFormat:@"第%d个cell",i]];
} [self.testCollectionView reloadData]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow: inSection:]; [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: inSection:];
[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: inSection:]; [self setupUICollectionView]; // 设置collectionView的数据
[self setupCollectionViewData];
} #pragma mark - private Method
#pragma mark 设置collectionView的数据
- (void)setupCollectionViewData { for (int i = ; i < ; i++) {
[self.dataArrayM addObject:[NSString stringWithFormat:@"第%d个cell",i]];
} [self.testCollectionView reloadData]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow: inSection:]; [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 = ;
flowLayout.minimumInteritemSpacing = ;
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 ;
} - (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(, );
} - (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
UICollectionView设置首个cell默认选中(二)的更多相关文章
- UICollectionView设置首个cell默认选中
设置UICollectionView中某个cell的默认选中,刚开始为追求性能,采用同一个cellId去标识UICollectionViewCell,却由于cell的重用会导致之前选中的cell在被重 ...
- 【坑】tableView cell默认选中
在tableView展示的过程时候,如果想一开始就有一些cell默认被选中,不能在cellForRowAtIndexPath中cell.selected=YES, 必须在willDisplayCell ...
- 用jQuery的attr()设置option默认选中无效的解决 attr设置属性失效
表单下拉选项使用selected设置,发现第一次默认选中成功,在页面不刷新的情况下,再次下拉,selected属性设置了,默认选中不生效 在手机端有些浏览器用jQuery的attr()方法设置sele ...
- 关于在layui中的table checkbox 默认选中设置
一.layui版本 layui-v2.4.5 二.设置table的checkbox默认选中 总共有两种方法: 方法1:在返回的json中设置LAY_CHECKED为true,页面上的checkbox就 ...
- iOS设置UITableView中Cell被默认选中后怎么触发didselect事件
//默认选中某个cell [self.searchResultTV selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] a ...
- vue中select设置默认选中
vue中select设置默认选中 一.总结 一句话总结: 通过v-model来:select上v-model的值为option默认选中的那项的值(value) 二.select设置默认选中实例 < ...
- HTML中的<select>标签如何设置默认选中的选项
方法有两种. 第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果. 1 2 3 4 5 < select id = " ...
- struts2设置<s:select>默认选中项的方法
struts2的select标签中,常用的有以下几个属性:(1)struts2中的select 标签中,必须设置的属性只有一个,即是list.(2)select标签的list中必须有值,不然会报错.如 ...
- Android RadioGroup中设置默认选中RadioButton 后,选中两个的问题 解决方法
项目中遇到多个RadioGroup中单选RadioButton ,设置了默认选中第一个 . 然后就 能选中两个RadioButton . . .. 我开始这样给设置默认选中一个的: for (int ...
随机推荐
- delphi 10 seattle 安卓服务开发(一)
从delphi 开始支持安卓的开发开始, 安卓service 开发一直都是delphier 绕不过去的坎, 以前也有开发service 的方法,但是都是手工处理启动文件,而且要修改很多东西,基本上成 ...
- mysql的一些配置优化
[mysqld]lower_case_table_names=1datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Di ...
- dex2jar 和 jd-gui 的安装与使用(转)
出处:https://blog.csdn.net/katrinawj/article/details/80016315 将APK直接解压(修改后缀名为.zip,然后解压)后,可以看到目录下包含一个cl ...
- 【MVC】bootstrap-paginator 分页
[MVC]bootstrap-paginator 分页http://www.cnblogs.com/stoneniqiu/p/4000041.htmlhttp://www.cnblogs.com/Le ...
- C++STL list
list双向链表 高效进行插入删除数据 不可以随机存取元素,所以不支持at()和[]操作符.it可以++ --,不能it+5 节点序号从0开始 list<int> l; l.push_b ...
- mysql学习之路_外键
回顾4 连接查询: 连接多张表到一起,不管记录数如何,字段数一定会增加. 分类:内连接,外连接.自然连接,交叉连接, 交叉连接:cross join (笛卡尔积) 内连接:inner join,左右两 ...
- MapGIS SDK(C++)【基础篇】
算法测试:Demo.Test https://www.cnblogs.com/2008nmj/p/10060847.html //例1-1 简单要素 void AppendSFeature(CSFea ...
- uva1659(最大费用循环流)
紫书上的一道题,做法见紫书P378,这篇博客用的第二种方法,关于正确性的证明,画图可以发现如果一个环是负环,跑最小费用流跑出的是环上的所有正边,再减去负边和即为跑一遍的负权,如果是正环,最小费用流即为 ...
- HDU 2546 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=2546 经典的01背包 预留5元买最贵的,剩余的就是01背包. #include<stdio.h> # ...
- 23种设计模式(1)-Facade设计模式
前记 曾经我遇见的一个需求是这样的,接口A有个方法void methodA(),类B需要实现接口A的methodA()方法,并且在类B中需要把methodA()方法内部处理逻辑获得的结果利用C类实例的 ...