我有以下代码来计算表(tableview1)中选定行的数量。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
int count = 0;
selectedindex1 = indexPath.row;
for (NSIndexPath *indexPath in tableview1.indexPathsForSelectedRows) {
count = count + 1;
}
rowcount = count;
}
其中 selectedindex1 和 rowcount 是整数变量。
只要您假设用户不会选择已选择的行,此代码就可以工作。如果这样做,应用程序将无法判断正确的选定行数,因为这样的操作不会触发 didSelectRowAtIndexPath 方法。有没有更好的方法来计算所选行的数量?
谢谢您的帮助。