我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
检查你的viewController是否有以下方法:
- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
如果你返回“NO”,didSelectRow将不会被调用
其他回答
您必须选择这些选项
但是如果你想让uitableview在点击时不突出显示,那么你应该改变UITableViewCell属性。
选择“选择”的“无”选项,如下所示
对于Xcode 6.4, Swift 1.2。在IB中选择“标签”被改变了。我不知道是怎么改变的,为什么改变。将其设置为“单一选择”使我的表格视图单元格再次可选。
添加@interface ExampleViewController () <UITableViewDelegate, UITableViewDataSource> 在故事板中进行委托
添加代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSString *cellText = cell.textLabel.text; }
即使另一个答案已经被接受,我将为观察这个问题的人补充一个可能的问题和解决方案:
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.
解决方案是在其他地方使用强引用跟踪控制器,直到确定不再需要它为止。
tableView?.allowsSelection = true
在Xcode 11.7中默认为False