简体   繁体   English

didDeselectRowAtIndexPath未被调用

[英]didDeselectRowAtIndexPath isn't called

I've implemented the didDeselectRowAtIndexPath method in a UITableView which includes multiple selection. 我在UITableView实现了didDeselectRowAtIndexPath方法,其中包括多个选择。

For some reason, didDeselectRowAtIndexPath is not being called by the delegate. 出于某种原因, didDeselectRowAtIndexPath不会调用didDeselectRowAtIndexPath Any suggestions? 有什么建议么? (I haven't accidentally misspelled didSelectRowAtIndexPath ). (我没有意外拼错didSelectRowAtIndexPath )。

Thanks! 谢谢!

Some things to note about cell selection: 有关细胞选择的一些注意事项:

  • didDeselectRowAtIndexPath is only called in a single-selection UITableView if the user clicks a different cell than the one already selected. didDeselectRowAtIndexPath仅在单选UITableView调用,如果用户单击与已选择的单元格不同的单元格。
  • didSelectRowAtIndexPath is called to select the new row, and didDeselectRowAtIndexPath is called to deselect the previous row 调用didSelectRowAtIndexPath来选择新行,调用didDeselectRowAtIndexPath来取消选择前一行
  • In a UITableView with multiple-selection, the previous row is not being deselected, so your deselect is handled in didSelectRowAtIndexPath 在具有多选的UITableView ,未取消选择上一行,因此在didSelectRowAtIndexPath处理取消选择

You can grab the cell using the delegate method you've implemented and modify it after checking it's selection like so: 你可以使用你实现的委托方法获取单元格,并在检查它的选择后修改它,如下所示:

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

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

I currently use a tableView with multiple selection, and didDeselectRowAtIndexPath method is calling for me (unlike what is stated in third bullet point in answer above). 我目前使用带有多个选择的tableView,并且didDeselectRowAtIndexPath方法正在调用我(不同于上面答案中的第三个项目符号中所述)。

I did note that cells that were "preselected", did not call didDeselectRowAtIndexPath correctly unless I did three things: 我注意到“预选”的单元格没有正确调用didDeselectRowAtIndexPath,除非我做了三件事:

  1. In cellForRowAtIndexPath, when I find a cell I want to preselect (you might check for matching array entries, or a string like I did), set cell.selected = true for that cell 在cellForRowAtIndexPath中,当我找到一个我想要预选的单元格时(您可能会检查匹配的数组条目,或者像我一样检查字符串),为该单元格设置cell.selected = true
  2. In cellForRowAtIndexPath, also make a call to method selectRowAtIndexPath for the cell to preselect 在cellForRowAtIndexPath中,还要调用方法selectRowAtIndexPath以使单元格预选
  3. In Interface Builder, uncheck the "Show Selection on Touch" checkmark for the tableView. 在Interface Builder中,取消选中tableView的“在触摸时显示选择”复选标记。 Oddly enough, this was required to ensure that when a preselected item was tapped (ie should be deselected), that didDeselectRowAtIndexPath was called correctly. 奇怪的是,这需要确保在点击预选项目(即应该取消选择)时,正确调用了didDeselectRowAtIndexPath。 在此输入图像描述

Example code: 示例代码:

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

I hope this helps someone who is trying to implement multiple selection, with preselected cells. 我希望这可以帮助那些尝试使用预选单元格实现多重选择的人。

If I'm not mistaken, UITableView doesn't have a built in method for deselecting cells. 如果我没弄错的话, UITableView没有用于取消选择单元格的内置方法。 I believe you need to call deselectRowAtIndexPath: in order for didDeselectRowAtIndexPath to be called. 我相信你需要调用deselectRowAtIndexPath:以便调用didDeselectRowAtIndexPath

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

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