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

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


当前回答

在我的情况下,问题是我有一个UITableViewCell子类,我实现了这两个方法: touchesBegan:withEvent: & touchesEnded:withEvent处理一个花哨的触摸动画。 但是我忘记添加[super touchesBegan: touchwithevent:event];方法ad [super touchesEnded: touchwithevent:event];也通知父单元的触摸。

所以将代码更改为以下解决了我的问题:

-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    [super touchesBegan:touches withEvent:event];
    //blah blah blah
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    //rest of the code
}

其他回答

这可能只是在我的情况下,但我从备份中重新加载了一些文件,一些东西不能工作,包括这个。在做了完全清洁(产品>清洁或Shift +命令+ K)后,它工作了。可能是在预编译的头文件中搞砸了。这对你来说可能不是问题,但值得一试。

在我的情况下,我犯了一个小错误,我分配:

tableView.isUserInteractionEnabled = false

它应该是:

tableView.isUserInteractionEnabled = true

我不能评论,写在这里。 在我的情况下didSelectRow工作,但没有didDeselectRow。 我为tableView设置委托和数据源,这解决了我的情况。

即使另一个答案已经被接受,我将为观察这个问题的人补充一个可能的问题和解决方案:

If you have automatic reference counting (ARC) turned on, you may find that even after assigning your controller as a delegate of the view, the view's messages to the controller are not being received because ARC is deleting the controller. Apparently the UITableView's delegate pointer does not count as a reference for the ARC, so if that is the only reference to it, the controller will be dealloc'd. You can verify whether or not this is happening by implementing the dealloc method on the controller and setting a breakpoint or NSLog call there.

解决方案是在其他地方使用强引用跟踪控制器,直到确定不再需要它为止。

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

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

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

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

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

64位兼容性!摇动的拳头

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