我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
检查你的viewController是否有以下方法:
- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
如果你返回“NO”,didSelectRow将不会被调用
其他回答
另一个疯狂的可能性是:我在iPhone 5S上运行我的应用,我使用了:
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
就像我过去一直做的那样。
然而,我没有仔细查看我的编译器警告…它说我应该把上面这一行改为:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
64位兼容性!摇动的拳头
当心,万一你开始扯你的头发。
我看了所有的答案,非常同意。但我的情况完全不同。我为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)
}
希望这个解决方案能帮助到那些有困难的人!
我遇到了一个问题,在几个月没有看我的代码后,我忘记了我实现了以下方法,由于一些不必要的需求
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
为了使行被选中,它应该返回YES。
记住在viewDidLoad方法中设置数据源和委托,如下所示:
[self.tableView setDelegate:self];
[self.tableView setDataSource:self];
听起来好像这个类不是表格视图的UITableViewDelegate,尽管UITableViewController应该自动设置。
你是否有机会将委托重置为其他类?