我有一个常规的 UIViewController
,我在 viewDidLoad
中添加了一个 UITableView
,如下所示:
dataTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 250, 320, 318)];
dataTableView.dataSource = self;
[self.view addSubview:dataTableView];
工作正常,单元格内容在那里,我通过调用 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
。我可以滚动和一切,但我无法让单元格对触摸/点击使用react。当我按下一个单元格时,它保持选中状态,但我似乎无法对其附加操作.我使用的代码是
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"select");
}
但它永远不会触发。我是否必须在 tableview 和调用之间建立某种连接?如果是这样,为什么它可以毫无问题地用于 cellForRowAtIndexPath
?任何帮助表示赞赏。我敢肯定,对于了解“Objective-C”的人来说,这很容易修复
最佳答案
您没有将自己设置为 dataTableView
的委托(delegate)。
dataTableView.delegate = self;
cellForRowAtIndexPath
起作用的原因是因为它是 dataSource
的一部分,而 didSelectRowAtIndexPath
是 delegate
的一部分.
关于ios - 将 UITableView 添加为 subview 时,didSelectRowAtIndexPath 不会触发单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615283/