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

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


当前回答

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

其他回答

我刚遇到这个问题,但它并没有马上发生;在选择了几个单元格之后,它们将停止调用didSelectItemAtIndexPath。我意识到问题是集合视图将allowmultipleselection设置为TRUE。因为单元格从未被取消选择,这阻止了未来调用didSelectItemAtIndexPath。

检查你的viewController是否有以下方法:

- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

如果你返回“NO”,didSelectRow将不会被调用

添加@interface ExampleViewController () <UITableViewDelegate, UITableViewDataSource> 在故事板中进行委托

添加代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSString *cellText = cell.textLabel.text; }

另一件可能导致问题的事情是不被选择的选择类型:

正常选择应为单选择,不应为无选择。

要以编程方式做到这一点,请执行:

tableView.allowsSelection = YES

如果你看了这个,那么问题还是没有解决。

我有自定义单元格,其中复选框“用户交互启用”是禁用的。所以,我只要打开它。祝你好运。