【问题标题】:UITableView Detect Selected Cell?UITableView检测选定的单元格?
【发布时间】:2011-05-18 03:09:36
【问题描述】:

我的应用中有多个 UITableView,有没有一种方法可以检测用户在该列中选择了哪个单元格/行?

还可以通过编程方式取消选择单元格/行吗?

谢谢。

【问题讨论】:

    标签: iphone objective-c ipad uitableview cell


    【解决方案1】:
    NSIndexPath *path = [tableView indexPathForSelectedRow];
    

    如果没有选择出售,上述行将抛出 EXC BAD ACCESS,因此请使用 NSIndexPath 实例变量跟踪您选择的单元格,并且仅在实际选择时使用 deselect isSelected 单元格的属性。

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:someIndexPath];
    if(cell.isSelected) {
        [tableView deselectRowAtIndexPath:someIndexPath animated:YES];
    }
    

    【讨论】:

      【解决方案2】:

      这些都可以在 UITableView 的文档中轻松找到。也许你下次应该先看看那里。

      这里甚至还有一个链接:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

      【讨论】:

        【解决方案3】:

        获取表当前选择的索引路径:

        NSIndexPath *path = [tableView indexPathForSelectedRow];
        

        取消选择当前选中的行:

        [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-10
          • 1970-01-01
          • 2011-04-27
          • 1970-01-01
          • 1970-01-01
          • 2010-11-05
          • 1970-01-01
          • 2021-08-28
          相关资源
          最近更新 更多