didDeselectRowAtIndexPath未被调用

时间:2015-02-10 18:48:45

标签: ios uitableview nsindexpath multipleselection

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

由于某种原因,代理人没有调用didDeselectRowAtIndexPath。有什么建议? (我没有意外拼错didSelectRowAtIndexPath)。

谢谢!

3 个答案:

答案 0 :(得分:10)

有关细胞选择的一些注意事项:

    只有当用户点击与已选择的单元格不同的单元格时,才会在单选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];
    }
}

答案 1 :(得分:2)

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

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

  1. 在cellForRowAtIndexPath中,当我找到一个我想要预选的单元格(您可能会检查匹配的数组条目,或者像我一样检查字符串)时,为该单元格设置cell.selected = true
  2. 在cellForRowAtIndexPath中,也可以调用方法 selectRowAtIndexPath,用于预选单元格
  3. 在Interface Builder中,取消选中tableView的“触摸时显示选择”复选标记。奇怪的是,这需要确保在点击预选项目(即应该取消选择)时,正确调用了didDeselectRowAtIndexPath。 enter image description here
  4. 示例代码:

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

    我希望这可以帮助那些尝试使用预选单元格实现多重选择的人。

答案 2 :(得分:0)

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