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

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


当前回答

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

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

其他回答

对此发表我的意见。

我有一个自定义UITableViewCell,有一个按钮覆盖整个单元格,所以当触摸发生时,按钮被选中,而不是单元格。

要么删除按钮,要么在我的情况下,我将按钮上的User interaction Enable设置为false,这样单元格就被选中了。

我遇到了一个问题,在几个月没有看我的代码后,我忘记了我实现了以下方法,由于一些不必要的需求

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

为了使行被选中,它应该返回YES。

我知道是旧的,问题已经解决了,但有类似的问题,我认为问题是与我的自定义UITableViewCell,但解决方案是完全不同的-我重新启动XCode:),然后工作正常!就像Windows一样:)

注意故事板中的UITableView属性,在我的例子中发生的是,我在故事板中选择了combox为“Selection: Single Selection”,这不允许didSelectRowAtIndexPath方法运行。

如果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