• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 当复选框被选中时,不会调用 didDeselectRowAtIndexPath

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:33:01 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用带有复选框的 UITableView。在我的 cellForRowAtIndexPath 方法中,我将选定的几个单元格设置如下,

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


if(tableView == self.sideBarTbl){
    cell.textLabel.text = [titleArray objectAtIndex:indexPath.row];
    cell.tag = indexPath.row;
    if(indexPath.row == 0){
        [cell setSelected:true];
    }else if(indexPath.row == 1){
        [cell setSelected:true];
    }else {
        [cell setSelected:false];
    }
}

简而言之,如果该行是 0,1,那么我检查 UITableView 的复选框。

当用户点击第 0 行或第 1 行时,它应该调用 didDeselectRowAtIndexPath,因为第 0 行和第 1 行的复选标记已经被选中。但是,它总是调用 didSelectRowAtIndexPath。不管复选框是否被选中,它总是调用didSelectRowAtIndexPath。如何让它在选中复选框时调用 didDeselectRowAtIndexPath 并在未选中复选框时调用 didSelectRowAtIndexPath



Best Answer-推荐答案


didDeselectRowAtIndexPath 方法只会为之前被触摸选择的单元格调用。它与单元格的 selected 属性无关。如果你想对取消选择的单元格做一些事情,只需使用 didSelectRowAtIndexPath 方法在 NSMutableArray 中手动记录选择。忘记 didDeselectRowAtIndexPath。供引用:UITableView cell on double click should go to previous state

还有一个 Objective-C 示例:

@interface SomeViewController : UIViewController<UITableViewDelegate> {
    NSMutableArray *selectedIndexPaths;
}

@implementation SomeViewController
//......
- (void)viewDidLoad {
    [super viewDidLoad];
    selectedIndexPaths = [NSMutableArray array];
}

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath {
    if ([selectedIndexPaths containsObject:indexPath]) {
        [selectedIndexPaths removeObject:indexPath];
        //Do something when deselecting.
    }
    else{
        [selectedIndexPaths addObject:indexPath];
        //Do something when selecting.
    }
}
//......
@end

如果选择是单选独占的,比如单选框,只需在处理前一个取消选择的过程后,使用单个变量NSIndexPath *selectedIndexPath;didSelectRowAtIndexPath中记录选择选定的单元格。

关于ios - 当复选框被选中时,不会调用 didDeselectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340983/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap