我尝试通过以下方式创建一个简单的清单样式 tableView:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([_selectedRows containsObject:indexPath]) {
[_selectedRows removeObject:indexPath];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
} else {
[_selectedRows addObject:indexPath];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
但是,当我选择第一个项目并滚动时,我发现同一部分中的所有第一个项目旁边都有复选标记。NSIndexPath 不包含行和节吗?是什么让所有项目都在这里被选中?:(
谢谢!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_IDENTIFIER];
}
if ([_selectedRows containsObject: indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
ABRecordRef selectedFriend = CFArrayGetValueAtIndex(_allFriends, [indexPath row]);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(selectedFriend, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(selectedFriend, kABPersonLastNameProperty));
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
return cell;
}