Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 alt text

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

like image 727
Christian Loncle Avatar asked Jan 06 '11 15:01

Christian Loncle


5 Answers

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];
like image 139
John Avatar answered Oct 12 '22 01:10

John


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.

like image 35
Alex Terente Avatar answered Oct 12 '22 01:10

Alex Terente


Play with

tableView.allowsMultipleSelection

This saved me hours of trouble. Found here.

like image 23
Eric Brotto Avatar answered Oct 12 '22 02:10

Eric Brotto


"Selected" rows in Clock application are cells with UITableViewCellAccessoryCheckmark accessory type.

Possible steps to achieve that are:

  1. Store checked/not checked state somewhere for each row.
  2. In cellForRowAtIndexPath: method for cell in checked row set cell's accessoryType property to UITableViewCellAccessoryCheckmark
  3. In cellForRowAtIndexPath: for cell in not checked row set cell's accessoryType property to UITableViewCellAccessoryNone
  4. When cell is selected (tapped) deselect it, invert its checked status and reload cell at that index path
like image 23
Vladimir Avatar answered Oct 12 '22 02:10

Vladimir


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.

like image 39
Faisal Memon Avatar answered Oct 12 '22 03:10

Faisal Memon