didSelectRowAtIndexPath selecting more than one row at a time

22,132

Here's one way to do it:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];        
    }

    [cell.textLabel setText:[NSString stringWithFormat:@"Row %d", indexPath.row]];

    NSIndexPath* selection = [tableView indexPathForSelectedRow];
    if (selection && selection.row == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;  
    }

    // Configure the cell.
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

Remember every cell in the table view is actually the same object being re-used. If you don't set the accessory type every time cellForRowAtIndexPath is called, when new cells scroll onto the screen they're going to all have the same accessory.

Multiple Select

For multiple selection it's a bit more complicated.

Your first option: Undocumented API

Note that this only works when the table's in editing mode. Set each cell's editing style to the undocumented UITableViewCellEditingStyleMultiSelect. Once you do that, you can get the table view's selection via an undocumented member of UITableView: indexPathsForSelectedRows. That should return an array of the selected cells.

You can expose this bit of functionality by putting this in a header:

enum {
    UITableViewCellEditingStyleMultiSelect = 3,
};

@interface UITableView (undocumented)
- (NSArray *)indexPathsForSelectedRows;
@end

Then set the editing style for each cell like so:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleMultiSelect;
}

When the table is in editing mode you'll see the multi-select controls on your cells.

To look through other undocumented API, you can use the nm command line utility like this:

nm /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/UIKit.framework/UIKit

Your second option: Manage the selection yourself

Have your UITableView subclass contain an array that indicates which cells are selected. Then in cellForRowAtIndexPath, configure the cell's appearance using that array. Your didSelectRowAtIndexPath method should then look something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView indexPathIsSelected:indexPath]) {
        [tableView removeIndexPathFromSelection:indexPath];
    } else {
        [tableView addIndexPathToSelection:indexPath];
    }
    // Update the cell's appearance somewhere here
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

This assumes you create indexPathIsSelected, removeIndexPathFromSelection, and addIndexPathToSelection methods in your UITableView subclass. These methods should do exactly what their names imply: Add, remove, and check for index paths in an array. You wouldn't need a didDeselectRowAtIndexPath implementation if you go with this option.

Share:
22,132
devsri
Author by

devsri

An enthusiastic learner of technology

Updated on October 11, 2020

Comments

  • devsri
    devsri over 3 years

    I have a UITableView populated with 27 rows. I am trying to change the accessoryType of the selected cell. I do that in the didSelectRowAtIndexPath: delegate method.

    The problem I am facing is that, when selecting a row and changing the accessoryType of the cell, the eleventh row from that row also gets modified.

    I have tried printing the [indexPath row] value, but it's showing only the row that was selected and not another one.

    I am really puzzled by such stuff; please help me out.

    ADDED THE CODE cellForRowAtIndexPath method

    UITableViewCell *cell;
    if ([indexPath row] == 0) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"acell"];
    }
    else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"bcell"];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (cell == nil && [indexPath row] != 0) {
        cell = [[[UITableViewCell alloc] initWithStyle:
                 UITableViewCellStyleSubtitle reuseIdentifier:@"bcell"] autorelease];
    }
    else if(cell == nil && [indexPath row] == 0){
        cell = [[[UITableViewCell alloc] initWithStyle:
                 UITableViewCellStyleSubtitle reuseIdentifier:@"acell"] autorelease];
    }
    
    if ([cell.contentView subviews]){
        for (UIView *subview in [cell.contentView subviews]) {
            [subview removeFromSuperview];
        }
    }
    if ([indexPath row] == 0) {
        cell.textLabel.text = @"Select All";
        cell.textLabel.font = [UIFont boldSystemFontOfSize:13.0f];
    }
    else {
        cell.textLabel.text = @"Some Text Here"
        cell.detailTextLabel.text = @"Another piece of text here"   
    }
    return cell;
    

    I am doing %10 because the behaviour is repeating after 11th row, hence trying to create new object for every 11th row.

    My didSelectRowAtIndexPath methos code is

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        if ([indexPath row] != 0) {
            NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:0 inSection:0];
            UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:tempIndex];
            tempCell.accessoryType = UITableViewCellAccessoryNone;
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    else{
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    if ([indexPath row] == 0) {
        for (int i = 0; i < [dataSource count]; i++) {
            NSIndexPath *tempIndex = [NSIndexPath indexPathForRow:i+1 inSection:0];
            UITableViewCell *tempCell = [tableView cellForRowAtIndexPath:tempIndex];
            if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
                tempCell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else{
                tempCell.accessoryType = UITableViewCellAccessoryNone;
            }
        }
    }
    

    Please help me in multiple selection or anyother way to solve the problem of multiple selection.

    Thanks in advance!!

  • devsri
    devsri about 13 years
    Hey thanks for the heads up, this code works perfectly fine if only single selection is to be done. What shall I change if I need to implement multiple selection? Thanks
  • daxnitro
    daxnitro about 13 years
    Just did some testing and revised the multi-select part a lot (the undocumented API stuff).
  • devsri
    devsri about 13 years
    Hey thanks for the efforts you made to help me, but I am still not getting through the problem. I have edited my question and added the code as well. Please see if that helps you in guiding me. Thanks..:)