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

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


当前回答

如果UITapGestureRecognizer出现问题,你可以修复这个:

在故事板:

在Objective-C代码中:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
[self.view addGestureRecognizer:tap];

[tap setCancelsTouchesInView:NO];

Swift代码:

let tap = UITapGestureRecognizer(target: self, action:Selector("dismissKeyboard"))
view.addGestureRecognizer(tap)

tap.cancelsTouchesInView = false

其他回答

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

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

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

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

另一个疯狂的可能性是:我在iPhone 5S上运行我的应用,我使用了:

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

就像我过去一直做的那样。

然而,我没有仔细查看我的编译器警告…它说我应该把上面这一行改为:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

64位兼容性!摇动的拳头

当心,万一你开始扯你的头发。

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

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

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

在我的例子中,我在加载时动态计算TableView的SuperView的高度。由于一个错误的计算,TableView被定位在SuperView之外。TableView绘制得很好,但是所有的交互都被禁用了(并且didSelectRowAtIndexPath从未被调用)。很难检测到,因为没有视觉上的迹象表明TableView是不可访问的。

tableView?.allowsSelection = true

在Xcode 11.7中默认为False