我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
注意故事板中的UITableView属性,在我的例子中发生的是,我在故事板中选择了combox为“Selection: Single Selection”,这不允许didSelectRowAtIndexPath方法运行。
其他回答
我看了所有的答案,非常同意。但我的情况完全不同。我为detailViewController创建了新的segue直接链接到StoryBoard中的表单元,这就导致了这个。我必须从我的单元格中移除那个segue并将它链接到UITableViewController本身。现在通过编写下面的代码,它可以工作,
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// Do any operation
performSegue(withIdentifier: "DetailSegue", sender: self)
}
希望这个解决方案能帮助到那些有困难的人!
添加@interface ExampleViewController () <UITableViewDelegate, UITableViewDataSource> 在故事板中进行委托
添加代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSString *cellText = cell.textLabel.text; }
如果你在UITableView上添加了一个gestureRecognizer, didSelectRowAtIndexPath将不会被调用。
你需要使用gestureRecognizer委托方法来避免特定视图中的触摸。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isDescendantOfView:YourTable]) {
return NO;
}
return YES;
}
以防有人和我犯同样愚蠢的错误
检查您所期望的didSelect的方法名是否会以某种方式意外地获得didDeselect。我花了大约两个小时才发现……
另一种可能性是UITapGestureRecognizer可以吃掉事件,就像这里的情况:https://stackoverflow.com/a/9248827/214070
我没有怀疑这个原因,因为表格单元格仍然会突出显示蓝色,就像水龙头通过一样。