开发者

Select multiple rows in UITableview [duplicate]

开发者 https://www.devze.com 2023-02-03 01:45 出处:网络
This question already has answers here: UITableView Multiple Selection (11 answers) 开发者_JS百科 Closed 5 years ago.
This question already has answers here: UITableView Multiple Selection (11 answers) 开发者_JS百科 Closed 5 years ago.

I would like to select multiple rows in a UITableview just like Apple does in the native Alarm app (see picture

Select multiple rows in UITableview [duplicate]

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:

  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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号