【发布时间】:2009-07-24 15:20:06
【问题描述】:
indexPath.row 在 tableView 中返回 null didSelectRowAtIndexPath:indexPath
我认为它应该返回选定的行。
当我在调试器中查看 indexPath 时,它(正确)返回:“2 个索引 [0, 0]”
我错过了什么吗?
【问题讨论】:
标签: iphone uitableview uikit
indexPath.row 在 tableView 中返回 null didSelectRowAtIndexPath:indexPath
我认为它应该返回选定的行。
当我在调试器中查看 indexPath 时,它(正确)返回:“2 个索引 [0, 0]”
我错过了什么吗?
【问题讨论】:
标签: iphone uitableview uikit
好吧null 是 0。 row 属性是 int 类型——也许你错误地将它用作对象或指针。
您提到您想要选择的实际“行”。如果您的意思是想要选中的 单元格,方法如下:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
请注意,如果您以某种方式以编程方式选择当前不可见的行,这将返回 nil。
【讨论】:
indexPath.section 告诉您它要求的部分。
indexPath.row 告诉您它想要该部分中的哪一行。
都从 0 开始。
你想做这样的事情:
if (indexPath.section == 0) { // 1st section
if (indexPath.row == 0) return cell1;
if (indexPath.row == 1) return cell2;
if (indexPath.row == 2) return cell3;
} else if (indexPath.section == 1) { // 2nd section
if (indexPath.row == 0) return cell4;
if (indexPath.row == 1) return cell5;
if (indexPath.row == 2) return cell6;
}
return nil;
【讨论】:
它将路径返回到所选行,而不是行(或单元格)本身。
indexPath(0,0) 是第一部分的第一个单元格。根据您正在运行的测试,这可能是正确的结果。
【讨论】: