UITableView&UICollectionView设置单元格的默认选中状态
1. 场景需求
一个表格视图(或者宫格视图)中,当一个单元格被选中时设置彩色样式,选中其它单元格时设置灰色样式。
2. 一个思路
通过实现选中和非选择的代理,以在适当的时机进行UI更新操作。
3. UITableView
3.1 通过屏幕点击改变的选中状态回调给代理
//选中-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
//非选中-(void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath;
3.2 代码设置默认选中状态 (要等数据加载完成之后再调用)
执行方法的主体:tableview对象
//选中-(void)selectRowAtIndexPath:(nullable NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
//非选中-(void)deselectRowAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;
注意的是:
上述代码强制设置某单元格选中或者不选中那一刻,都不会回调tableview的选中代理方法,也不会发出通知UITableViewSelectionDidChangeNotification。
之后,通过屏幕点击选中其它cell的时候,可以执行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
代理方法,你就有机会还原cell的默认样式。
3.3 补充:代码设置默认选中状态
执行方法的主体:cell对象
-(void)setSelected:(BOOL)selected animated:(BOOL)animated;
// animate between regular and selected state
注意的是:
这种方法改变cell的选中状态时,当通过屏幕点击选中其它cell的时候,UITableView并不会执行- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
的非选中代理方法,你也就没有机会还原cell的默认样式。
4. UICollectionView
4.1 通过屏幕点击改变的选中状态回调给代理
//选中-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath;
//非选中-(void)collectionView:(UICollectionView*)collectionView didDeselectItemAtIndexPath:(NSIndexPath*)indexPath;
4.2 代码设置默认选中状态 (要等数据加载完成之后再调用)
执行方法的主体:UICollectionView对象
//选中-(void)selectItemAtIndexPath:(nullable NSIndexPath*)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
//非选中-(void)deselectItemAtIndexPath:(NSIndexPath*)indexPath animated:(BOOL)animated;
注意的是:
类似的,上述代码强制设置某单元格选中或者不选中那一刻,都不会回调选中代理方法,也不会发出通知。
之后,通过屏幕点击选中其它cell的时候,可以执行- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
代理方法,你就有机会还原cell的默认样式。
4.3 补充:代码设置选中状态
执行方法的主体:cell对象
-(void)setSelected:(BOOL)selected;
注意的是:
类似的,这种方法改变cell的选中状态时,当屏幕选中其它cell的时候,UITableView并不会执行- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath ;
代理方法。
5. 比较
比如,下面两种方案
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:0inSection:0];
UICollectionViewCell*cell=[self.collectionView cellForItemAtIndexPath:indexPath];
[cell setSelected:YES];
上述方案仅仅改变cell的属性,但当屏幕点击选中其它cell的时候,也不会执行原cell的非选中代理。
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0inSection:0]animated:YES scrollPosition:UICollectionViewScrollPositionNone];
上述方案改变了cell的属性,而且当选中其它cell的时候,会执行非选中代理。
6. 手动执行代理
上述两张方案的区别在于,设置选中状态完后,屏幕点击其它cell时,一个执行原cell的didDeselect方法,一个不执行。
相同点在于,手动设置选中的时候,都是不会执行didSelect方法的。
如果你真的想在改变选中状态的时候执行didSelect代理,那么可以手动执行:
【UITableView&UICollectionView设置单元格的默认选中状态】NSIndexPath*indexPath=[NSIndexPath indexPathForRow:0inSection:0];
[mytableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:0inSection:0]animated:YES scrollPosition:UITableViewScrollPositionTop];
if([mytableview.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]){[mytableview.delegate tableView:mytableview didSelectRowAtIndexPath:indexPath];
}
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 宋仲基&宋慧乔(我们不公布恋情,我们直接结婚。)
- 21天|21天|M&M《见识》04
- 二叉树路径节点关键值和等于目标值(LeetCode--112&LeetCode--113)
- 2021—3—8日教练实践总结&呼吸练习&觉察日记
- #12-UITableView|#12-UITableView 优化方案
- 奇迹-妖妈|奇迹-妖妈 感恩日记46/365&非暴力沟通第3天
- 前端|web前端dya07--ES6高级语法的转化&render&vue与webpack&export
- 数据技术|一文了解Gauss数据库(开发历程、OLTP&OLAP特点、行式&列式存储,及与Oracle和AWS对比)
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置