UICollectionView设置首个cell默认选中
设置UICollectionView中某个cell的默认选中,刚开始为追求性能,采用同一个cellId去标识UICollectionViewCell,却由于cell的重用会导致之前选中的cell在被重用后并不会响应取消选中的代理方法,会造成多个cell选中的结果。 在- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath代理方法中根据每个indexPath.row去设置每个cell的id,防止cell重用,并保存在一个字典中,并把cell的注册方法放在该代理方法中代码如下:
NSString *identifier = [self.cellIdentifierDictM objectForKey:[NSString stringWithFormat:@"%@", indexPath]];
if(identifier == nil){
identifier = [NSString stringWithFormat:@"selectedCell%@", [NSString stringWithFormat:@"%zd", indexPath.row]];
[self.cellIdentifierDictM setObject:identifier forKey:[NSString stringWithFormat:@"%@",indexPath]];
// 注册Cell(把对cell的注册写在此处)
[collectionView registerClass:[LBCollectionFirstCell class] forCellWithReuseIdentifier:identifier];
}
由于我这边的数据是从网络上加载的,是再加载过数据并刷新之后,设置默认UICollectionView的第一个Cell选中代码如下:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow: inSection:];
[self collectionView:self.firstHeadView.catogeryCollectionView didSelectItemAtIndexPath:indexPath];
[self.firstHeadView.catogeryCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
这种方法对于较多cell的话,性能有限,最后提醒不要忘了字典的初始化
UICollectionView设置首个cell默认选中的更多相关文章
- UICollectionView设置首个cell默认选中(二)
上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制.对于较少的数据情况是可以的,但是对于数据比较大 ...
- 【坑】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 ...
- 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 ...
- 浅析iOS tableview的selectRowAtIndexPath选中无效(默认选中cell无效)
可能很多人都遇到过这种情况: tableview列表,有时加载完,需要默认选中某一行,给予选中效果:或者需要执行某行的点击事件. 我们举例: 比如我想默认选中第一行 可能我们第一个想法就是这样: [m ...
随机推荐
- javascript捕获页面窗口关闭事件
javascript捕获窗口关闭事件有两种方法 onbeforeunload() ,onUnload() 用法有两种: 1. function window.onbefore ...
- Linux关机操作
正确的关机流程为:sync > shutdown > reboot > halt 关机指令为:shutdown ,你可以man shutdown 来看一下帮助文档. 例如你可以运行如 ...
- springboot深入学习(一)-----springboot核心、配置文件加载、日志配置
一.@SpringBootApplication @SpringBootApplication是spring boot的核心注解,源码如下: 相当于:@Configuration+@EnableAut ...
- Le Chapitre V
Chaque jour j'apprennais quelque chose sur la planète, sur le départ, sur le voyage. Ca venait tout ...
- (4)4 larger-than-life lessons from soap operas
https://www.ted.com/talks/kate_adams_4_larger_than_life_lessons_from_soap_operas/transcript 00:12In ...
- excel2007vba绘图1
参考:http://club.excelhome.net/thread-480025-1-1.html '----------------------------------------------- ...
- Linux无法解析gitlib的地址--修改dns
搞的一个js鉴权认证,先跳转到 gitlib,登录后跳转到我们公司测试接口的页面: 公司gitlib地址:gitlab.cmread.com [INFO][2018-12-17 15:29:00,18 ...
- 虚拟机CentOs的安装及大数据的环境搭建
大数据问题汇总 1.安装问题 1.安装步骤,详见文档<centos虚拟机安装指南> 2.vi编辑器使用问题,详见文档<linux常用命令.pd ...
- Keras人工神经网络多分类(SGD)
import numpy as np import pandas as pd from keras.models import Sequential from keras.layers import ...
- 2.2.1synchronized方法的弊端
缺陷:用关键字synchronized声明方法是有弊端的,譬如A线程调用同步方法执行一个长时间的任务,那么B线程则必须等待较长的时间, 解决方法:使用synchronized同步语句块 package ...