为什么没有调用-didDeselectRowAtIndexPath?

phi*_*phi 24 uitableview ios

我创建了一个新项目(Xcode 4,Master-Detail应用程序),看看我做错了什么,但我仍然遇到同样的问题.我想-reloadData在用户取消选择单元格时调用,所以这是我的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [tableView reloadData];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return indexPath;
}
Run Code Online (Sandbox Code Playgroud)

问题是,似乎didDeselectRowAtIndexPathwillDeselectRowAtIndexPath没有被调用.这是预期的行为吗?tableView:didDeselectRowAtIndexPath:国家的文件

告诉代理现在取消选择指定的行.

所以我猜它应该像我想的那样工作.

fre*_*tag 52

如果您调用deselectRowAtIndexPath:animated:,则不会发送委托方法tableView:willDeselectRowAtIndexPath:tableView:didDeselectRowAtIndexPath:消息.


ber*_*ium 23

文件tableView:willDeselectRowAtIndexPath:也说

仅当用户尝试选择其他行时存在现有选择时,才会调用此方法.代理将为先前选定的行发送此方法.您可以使用 UITableViewCellSelectionStyleNone在触摸时禁用单元格高亮显示的外观.

我用的时候不适用于我们UITableViewCellSelectionStyleNone.

  • 这个答案看起来不错.`UITableViewCellSelectionStyleNone`不会阻止调用`willDeselectRowAtIndexPath`和`didDeselectRowAtIndexPath`. (5认同)

And*_*rew 12

我发现的另一个怪癖 - 无论如何在IOS 5.1中 - 就是如果你reloadData在桌子上打电话,你将无法获得didDeselectRowAtIndexPath任何选定的行.在我的情况下,我根据它是否被选中稍微调整单元格布局,所以我需要在重新加载表格数据之前手动完成这项工作.


Jam*_*mes 8

我知道这是一个老问题,但我遇到了同样的问题.

如果你使用

[tableView reloadData]
Run Code Online (Sandbox Code Playgroud)

然后重新加载表数据,并且在幕后没有选择任何行 - 意思是

只要

didSelectRowAtIndexPath
Run Code Online (Sandbox Code Playgroud)

被称为.我希望这可以帮助遇到这个问题的人.


kev*_*lek 5

解决此问题的一种干净方法是执行以下操作:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
    // Do your cell setup work here...

    //If your cell should be selected...
    if cellShouldBeSelected {
        tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
    }

    return cell
}
Run Code Online (Sandbox Code Playgroud)

这解决了整个问题,即在发生tableView.reloadData()呼叫后,小区不响应被取消选择。