简体   繁体   中英

iOS tableView multiple selection

Many of the ways and methods out there to select multiple rows is either deprecated or doesn't work.

I simply want to select multiple rows in didSelectRowAtIndexPath and save them into Array or NSUserDefault . This case I picked NSUserDefault but I am willing to change it to Array.

This is my code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

    //set clicked icon after row selected
    cell.imgIcon.image=[UIImage imageNamed:@"select-icon"];

    //notification id for selected row
    Notifications* selected = notifications[indexPath.section];
    selectedGroup = selected.NotificationsId;

     //save it in nsuserdefaults
    [[NSUserDefaults standardUserDefaults] setObject:selectedGroup forKey:@"notificationSelectedID"];

}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

//deselect the row

}

Note : I am using latest Xcode SDK 8.

Thank you for your helps.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];

 // do your logic with following condition

if (cell.accessoryType == UITableViewCellAccessoryNone) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    } 
    else 
    {

        cell.accessoryType = UITableViewCellAccessoryNone;

    }

}

You can declare mutable array and when every time user selects particular row, add required detail for particular row in an array then use NSUserDefaults to store updated array.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[arrRows addObject:[NSString stringWithFormat:@"%@", indexPath]];
[[NSUserDefaults standardUserDefaults] setValue:arrRows forKey:@"arrRows"];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM