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 ...
随机推荐
- windows无法卸载jdk的解决方法
装了java之后非常纠结的就是无法卸载,总不能因为卸载一个jdk去重装系统,但是看着它残存在那又非常不爽, 因为卸载会牵扯注册表等琐碎的东西,,,后来在官网发现神器一枚,此神器就是java卸载工具. ...
- thinkphp5 数据库和模型
1.Db和模型的存在只是ThinkPHP5.0架构设计中的职责和定位不同,Db负责的只是数据(表)访问,模型负责的是业务数据和业务逻辑.2.Db和模型最明显的一个区别就是Db查询返回的数据类型为数组( ...
- windows下解决端口被占用的问题
步骤一.Windows查看所有的端口 点击电脑左下角的开始,然后选择运行选项,接着我们在弹出的窗口中,输入[cmd]命令,进行命令提示符.然后我们在窗口中输入[netstat -ano]按下回车,即会 ...
- Android APP测试流程
一. Monkey测试(冒烟测试) 使用monkey测试工具进行如下操作: 1. APP的安装 2. APP随机操作测试(APP压力测试) 3. APP的卸载 二. 安装卸载测试 1. 使用测试真机进 ...
- Win7 VS2013环境编译Squirrel 3.0.7
Squirrel是一个类似Lua,但是更面向对象的脚本语言. 国内这个介绍很少,环境配置更是没有任何文章提到,花了点时间搞定了,备忘记录下过程. 首先是下载,写本文时Squirrel最新版本为3.0. ...
- High-radix routers
The idea is to reduce H (hops), by adding explicit links between physically distant routers, thus re ...
- C# 编码标准(三)
一.代码注释 1.文档型注释 该类注释采用.Net已定义好的Xml标签来标记,在声明接口.类.方法.属性.字段都应该使用该类注释,以便代码完成后直接生成代码文档,让别人更好的了解代码的实现和接口.[示 ...
- MongoDB-增删改
MongoDB的shell使用了Js引擎,因此能运行任意的Js程序. MongoDB中常用基本数据类型: null:空值或者不存在的字段Boolean:true,false数值型:{"x&q ...
- 成功解决在Python文件上右键菜单无“Edit with IDLE”选项
我电脑是Win7旗舰版,之前电脑上安装的是Python2.6版本的,前两天为了体验一下Microsoft Excel与Python之间互操作, 下载并安装了DataNitro,在安装的时候脑残的安装了 ...
- linux上搭建solr(用jetty部署)
环境搭建:centos7及solr7版本 描述:最新版本的solr内置了jetty容器,可以支持jetty部署,从而不需要发布到tomcat下面 首先同样先在/usr/local/mypackage上 ...