【问题标题】:didDeselectRowAtIndexPath isn't called没有调用 didDeselectRowAtIndexPath
【发布时间】:2015-04-10 22:38:57
【问题描述】:

我在UITableView 中实现了didDeselectRowAtIndexPath 方法,其中包括多项选择。

由于某种原因,didDeselectRowAtIndexPath 没有被委托调用。有什么建议? (我没有不小心拼错了didSelectRowAtIndexPath)。

谢谢!

【问题讨论】:

  • 您是否将 UITableView 委托设置为指向包含 didSelectRowAtIndexPath 的对象? tableView.delegate = self; 之类的东西?

标签: ios uitableview nsindexpath multipleselection


【解决方案1】:

关于单元格选择的一些注意事项:

  • didDeselectRowAtIndexPath 仅在用户单击与已选择的单元格不同的单元格时在单选 UITableView 中调用。
  • 调用didSelectRowAtIndexPath 选择新行,调用didDeselectRowAtIndexPath 取消选择上一行
  • 在具有多项选择的UITableView 中,前一行没有被取消选择,因此您的取消选择在didSelectRowAtIndexPath 中处理

您可以使用已实现的委托方法获取单元格,并在检查其选择后对其进行修改,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.selected) {
        // ... Uncheck
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

【讨论】:

    【解决方案2】:

    我目前使用具有多项选择的 tableView,并且 didDeselectRowAtIndexPath 方法正在调用我(与上面答案中的第三个要点中所述不同)。

    我确实注意到“预选”的单元格没有正确调用 didDeselectRowAtIndexPath,除非我做了三件事:

    1. 在 cellForRowAtIndexPath 中,当我找到要预选的单元格时(您可以检查匹配的数组条目,或者像我一样检查字符串),为该单元格设置 cell.selected = true
    2. 在 cellForRowAtIndexPath 中,也调用方法 selectRowAtIndexPath 用于预选单元格
    3. 在 Interface Builder 中,取消选中 tableView 的“Show Selection on Touch”复选标记。奇怪的是,这需要确保当一个预选项目被点击(即应该被取消选择)时,didDeselectRowAtIndexPath 被正确调用。

    示例代码:

    if productsReceived!.rangeOfString(cell.productName.text!) != nil {
        cell.accessoryType = .Checkmark 
        tableView.selectRowAtIndexPath(indexPath, animated: true,   scrollPosition: UITableViewScrollPosition.None)
        cell.selected = true
    }
    

    我希望这对尝试使用预选单元格实现多选的人有所帮助。

    【讨论】:

      【解决方案3】:

      如果我没记错的话,UITableView 没有用于取消选择单元格的内置方法。我相信您需要致电deselectRowAtIndexPath: 才能调用didDeselectRowAtIndexPath

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多