繁体   English   中英

didDeselectRowAtIndexPath未被调用

[英]didDeselectRowAtIndexPath isn't called

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

出于某种原因, didDeselectRowAtIndexPath不会调用didDeselectRowAtIndexPath 有什么建议么? (我没有意外拼错didSelectRowAtIndexPath )。

谢谢!

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

  • 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];
    }
}

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

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

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

示例代码:

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

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

如果我没弄错的话, UITableView没有用于取消选择单元格的内置方法。 我相信你需要调用deselectRowAtIndexPath:以便调用didDeselectRowAtIndexPath

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM