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

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


当前回答

在我的情况下,问题是我有一个UITableViewCell子类,我实现了这两个方法: touchesBegan:withEvent: & touchesEnded:withEvent处理一个花哨的触摸动画。 但是我忘记添加[super touchesBegan: touchwithevent:event];方法ad [super touchesEnded: touchwithevent:event];也通知父单元的触摸。

所以将代码更改为以下解决了我的问题:

-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    [super touchesBegan:touches withEvent:event];
    //blah blah blah
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    //rest of the code
}

其他回答

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

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

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

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

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

好的,在这里更新,因为我刚刚遇到了这个问题,我的问题与这里发现的略有不同。

我在IB中查看,看到我的委托被设置,但它被错误地设置为视图,而不是文件的所有者(右键单击表视图,查看委托指向的位置)。

希望这能帮助到别人

如果您所单击的单元格被突出显示,但函数仍然没有被调用,请再次检查函数的签名。它应该是这样的:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

我刚刚找到了另一种不调用didSelect方法的方法。在某种程度上,可能是在func声明本身的一些错误中,XCode建议我将@nonobjc添加到我的方法中:

@nonobjc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

这将继续编译没有抱怨,但你永远不会被ui动作调用。

“那是我的两分钱,我想要回我的零钱”