我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。

有没有人知道我到底搞砸了什么才让这一切发生的?


当前回答

在我的情况下,didSelctRowAtIndexPath不调用是因为我在tableView的Selection属性中没有选择,设置为单一选择解决了我的问题

其他回答

记住在viewDidLoad方法中设置数据源和委托,如下所示:

[self.tableView setDelegate:self];

[self.tableView setDataSource:self];

确保你实现了tableView:didSelectRowAtIndexPath而不是tableView:didDeSelectRowAtIndexPath

这让我在很多场合!!

如果您的表视图处于编辑模式(例如。[tableView setEditing:YES animated:NO];),你需要设置tableView。allowsSelectionDuringEditing = YES;

您可能犯的另一个错误(就像我犯的那样):如果在单元格中设置segue,则不会调用didSelectRowAtIndexPath。你应该在视图控制器中设置segue。

我也遇到过同样的问题。而且很难找到。但在我的代码中 是这样的:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

它必须返回indexPath,否则-tableView:didSelectRowAtIndexPath:没有被调用。