我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
我正在编写一个带有tab视图中的表视图的iOS应用程序。在我的UITableViewController中,我实现了-tableView:didSelectRowAtIndexPath:,但当我在运行时选择一行时,该方法没有被调用。表格视图正在被填充,所以我知道控制器中的其他tableView方法正在被调用。
有没有人知道我到底搞砸了什么才让这一切发生的?
当前回答
我刚刚有了这个,就像过去发生在我身上的一样,它没有工作,因为我在尝试添加方法时没有注意自动完成,我实际上最终实现了tableView:didDeselectRowAtIndexPath:而不是tableView:didSelectRowAtIndexPath:。
其他回答
检查你的viewController是否有以下方法:
- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
如果你返回“NO”,didSelectRow将不会被调用
如果您的表视图处于编辑模式(例如。[tableView setEditing:YES animated:NO];),你需要设置tableView。allowsSelectionDuringEditing = YES;
另一种可能性是UITapGestureRecognizer可以吃掉事件,就像这里的情况:https://stackoverflow.com/a/9248827/214070
我没有怀疑这个原因,因为表格单元格仍然会突出显示蓝色,就像水龙头通过一样。
我刚刚找到了另一种不调用didSelect方法的方法。在某种程度上,可能是在func声明本身的一些错误中,XCode建议我将@nonobjc添加到我的方法中:
@nonobjc func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
这将继续编译没有抱怨,但你永远不会被ui动作调用。
“那是我的两分钱,我想要回我的零钱”
添加@interface ExampleViewController () <UITableViewDelegate, UITableViewDataSource> 在故事板中进行委托
添加代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; NSString *cellText = cell.textLabel.text; }