How to select Multiple rows in UITableView?
I want multiple selection in UITableView and also its data into one array or i开发者_开发问答n one dictionary...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
selectedIndexes
has to be an array where you manage the selected cells indexes and utilize later accordingly.
Trying to do the same thing, but I'm seeing the "selected" checkmark accessory on other lines that were never selected when you scroll down the list. Also, deselecting one of the "phantom" items down the list causes the accessory to go away in the upper part of the list -- but the array is unaffected.
Here's how I have it implemented:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(@"IVC selectedIndexes AddObject @ %d:", indexPath.row);
NSLog(@"IVC selectedIndexes: %@", selectedIndexes);
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
NSLog(@"IVC selectedIndexes RemoveObject @ %d:", indexPath.row);
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
EDIT: Added solution: You need to put this in your "cellForRowAtIndexPath" code:
if ([selectedIndexes containsObject:[NSNumber numberWithInt:indexPath.row]]) [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
精彩评论