前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >iOS小经验:UITableView&UICollectionView设置单元格的默认选中状态

iOS小经验:UITableView&UICollectionView设置单元格的默认选中状态

作者头像
陈满iOS
发布于 2018-10-09 03:48:06
发布于 2018-10-09 03:48:06
3.8K00
代码可运行
举报
文章被收录于专栏:陈满iOS陈满iOS
运行总次数:0
代码可运行

本文属 iOS小经验系列:累积平时看起来简单,容易忽视的边边角角,各路大佬敬请回避。

1. 场景需求

一个表格视图(或者宫格视图)中,当一个单元格被选中时设置彩色样式,选中其它单元格时设置灰色样式。

2. 一个思路

通过实现选中和非选择的代理,以在适当的时机进行UI更新操作。

3. UITableView
3.1 通过屏幕点击改变的选中状态回调给代理
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
//非选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
3.2 代码设置默认选中状态 (要等数据加载完成之后再调用)

执行方法的主体:tableview对象

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//选中
- (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对象

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (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 通过屏幕点击改变的选中状态回调给代理
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//选中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//非选中
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
4.2 代码设置默认选中状态 (要等数据加载完成之后再调用)

执行方法的主体:UICollectionView对象

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//选中
- (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对象

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (void)setSelected:(BOOL)selected;

注意的是:

  • 类似的,这种方法改变cell的选中状态时,当屏幕选中其它cell的时候,UITableView并不会执行- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath ;代理方法。
5. 比较

比如,下面两种方案

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    [cell setSelected:YES];

上述方案仅仅改变cell的属性,但当屏幕点击选中其它cell的时候,也不会执行原cell的非选中代理。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];

上述方案改变了cell的属性,而且当选中其它cell的时候,会执行非选中代理。

6. 手动执行代理
  • 上述两张方案的区别在于,设置选中状态完后,屏幕点击其它cell时,一个执行原cell的didDeselect方法,一个不执行。
  • 相同点在于,手动设置选中的时候,都是不会执行didSelect方法的。

如果你真的想在改变选中状态的时候执行didSelect代理,那么可以手动执行:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [mytableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
    if ([mytableview.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
        [mytableview.delegate tableView:mytableview didSelectRowAtIndexPath:indexPath];
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.09.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 场景需求
  • 2. 一个思路
  • 3. UITableView
    • 3.1 通过屏幕点击改变的选中状态回调给代理
    • 3.2 代码设置默认选中状态 (要等数据加载完成之后再调用)
    • 3.3 补充:代码设置默认选中状态
  • 4. UICollectionView
    • 4.1 通过屏幕点击改变的选中状态回调给代理
    • 4.2 代码设置默认选中状态 (要等数据加载完成之后再调用)
    • 4.3 补充:代码设置选中状态
  • 5. 比较
  • 6. 手动执行代理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档