首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITableView每次只标记一行

UITableView每次只标记一行
EN

Stack Overflow用户
提问于 2012-04-17 14:11:50
回答 18查看 52.9K关注 0票数 21

使用这段代码,我可以检查一个表中的多行。

但我想一次只检查一行。这是我的密码:

代码语言:javascript
运行
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (theCell.accessoryType == UITableViewCellAccessoryNone) {
        theCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else if (theCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        theCell.accessoryType = UITableViewCellAccessoryNone;
    }
}

如果用户选择一个不同的行,那么我希望旧行自动取消选中。不知道该怎么做。

EN

Stack Overflow用户

发布于 2017-08-17 07:58:53

测试并解决这个问题,尝试它完美地工作。

将其添加为全局变量

代码语言:javascript
运行
复制
var selectedIndexPath = NSIndexPath(row: 0, section: 0)

将此添加到didselect方法中

代码语言:javascript
运行
复制
        // old one check off
        if indexPath != selectedIndexPath as IndexPath {
            let oldCell = categorytable.cellForRow(at: selectedIndexPath as IndexPath)
            if oldCell?.accessoryView != nil {
                oldCell?.accessoryView = nil
            }
            else {
                let imageName = "checkmark"
                let image: UIImageView = UIImageView(image: UIImage(named: imageName))
                cell.accessoryView = image
            }
        }
        // the new one on

        if  cell.accessoryView == nil {
            let imageName = "checkmark"
            let image: UIImageView = UIImageView(image: UIImage(named: imageName))
            cell.accessoryView = image
        }
        else {
            cell.accessoryView = nil
        }
票数 0
EN
查看全部 18 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10192908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档