我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
添加@interface ExampleViewController () <UITableViewDelegate, UITableViewDataSource> 在故事板中进行委托
添加代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSString *cellText = cell.textLabel.text; }
其他回答
在我的情况下,didSelctRowAtIndexPath不调用是因为我在tableView的Selection属性中没有选择,设置为单一选择解决了我的问题
我不确定是否有人滚动到足够远的地方看到这个答案,但由于这是关于这个主题最受欢迎的问题,而答案并不在那里,我将添加它:
从Xcode 9 / Swift 4开始,所有Objective-C方法都应该标记为@objc。编译器做了一个合理的工作,识别它应该应用在哪里,但它不计算继承。例如:
class Delegate: NSObject, UITableViewDelegate {
}
class SuperDelegate: Delegate {
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}
这将不会产生任何警告、崩溃或构建错误。但是你的行不会被调用,直到你添加@objc:
@objc class SuperDelegate: Delegate {
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return indexPath }
}
我不能评论,写在这里。 在我的情况下didSelectRow工作,但没有didDeselectRow。 我为tableView设置委托和数据源,这解决了我的情况。
你必须检查这个 选择必须是单一选择,编辑必须是在编辑过程中没有选择 你也可以改变uttableviewcell属性中的设置 在表格视图单元格中编辑 样式必须是自定义的,标识符必须是Rid section为none
您可能犯的另一个错误(就像我犯的那样):如果在单元格中设置segue,则不会调用didSelectRowAtIndexPath。你应该在视图控制器中设置segue。