Select multiple rows in UITableview [duplicate]
I would like to select multiple rows in a UITableview just like Apple does in the native Alarm app (see picture
How can I do that?
All the answers I saw so far were old (and said "Can't do") or refer to the multiple row selection in the native Mail app. That method is not what I would like because you have to enter in the "edit"-mode first. I want to be able to select the rows right away.
Hope someone can help.
Christian
Easiest way i've found:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
You can see which cells are selected by calling:
NSArray *selectedCells = [self.tableView indexPathsForSelectedRows];
In
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
do
if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}else{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
Edit:
You also have to use an array that maps the checked cells and in cellForRowAtIndexPath you have to verify if the accessoryType should be checked or not.
Play with
tableView.allowsMultipleSelection
This saved me hours of trouble. Found here.
"Selected" rows in Clock application are cells with UITableViewCellAccessoryCheckmark
accessory type.
Possible steps to achieve that are:
- Store checked/not checked state somewhere for each row.
- In
cellForRowAtIndexPath:
method for cell in checked row set cell'saccessoryType
property toUITableViewCellAccessoryCheckmark
- In
cellForRowAtIndexPath:
for cell in not checked row set cell'saccessoryType
property toUITableViewCellAccessoryNone
- When cell is selected (tapped) deselect it, invert its checked status and reload cell at that index path
If you are using Storyboards, just click on the Table View and then on the Attributes Inspector on the right hand panel, set the "selection" property to "Multiple Selection". I've verified this using Xcode version 8.2.1.
精彩评论