Is it possible to configure a UITableView to allow multiple-selection?
Asked Answered
H

12

41

For the iPhone, is it possible to configure a UITableView such that it will allow multiple-selection?

I've tried overriding -setSelected:animated: for each UITableViewCell, but trying to fudge the required behavior is tricky as it's difficult to separate the real unselections from the ones where the UITableView thinks I've unselected due to selection of another cell!

Hope someone can help!

Thanks,

Nick.

Housum answered 21/11, 2008 at 6:57 Comment(0)
F
38

The best way to do this would be to a checkmark per selected row.

You can do that by setting the accessoryType on the selected UITableViewCell instances to UITableViewCelAccessoryCheckmark.

To deselect the row, set it back to UITableViewCellAccessoryNone.

To enumerate which cells/rows were selected (say, upon clicking a button), simply iterate over the cells of the table looking for UITableViewCellAccessoryCheckmark. Or, manage some NSSet or the like in your table view delegate in the "did select" delegate methods.

Footstone answered 21/11, 2008 at 16:39 Comment(2)
There's a nice example of using the "did select" delegate methods to keep track of the selected cells/rows here: (#6986407)Forefather
Iterating over the cells of the table isn't accurate since table cells are often recycled and reused as you scroll through your table.Gertrudegertrudis
T
40

Following property should work fine if you are developing app for iOS5.0+

self.tableView.allowsMultipleSelection = YES;
Timisoara answered 1/4, 2012 at 23:12 Comment(2)
this is the standard iOS way of handling multiple selections in a tableView. see also Apple's sample developer.apple.com/library/ios/samplecode/TableMultiSelect/…Elviselvish
@Elviselvish not at the time when the question was asked.Timisoara
F
38

The best way to do this would be to a checkmark per selected row.

You can do that by setting the accessoryType on the selected UITableViewCell instances to UITableViewCelAccessoryCheckmark.

To deselect the row, set it back to UITableViewCellAccessoryNone.

To enumerate which cells/rows were selected (say, upon clicking a button), simply iterate over the cells of the table looking for UITableViewCellAccessoryCheckmark. Or, manage some NSSet or the like in your table view delegate in the "did select" delegate methods.

Footstone answered 21/11, 2008 at 16:39 Comment(2)
There's a nice example of using the "did select" delegate methods to keep track of the selected cells/rows here: (#6986407)Forefather
Iterating over the cells of the table isn't accurate since table cells are often recycled and reused as you scroll through your table.Gertrudegertrudis
K
25

Use the following code to set up the cell accesory types:

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

    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


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

    }else{
        thisCell.accessoryType = UITableViewCellAccessoryNone;

    }
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {

//add your own code to set the cell accesory type.
return UITableViewCellAccessoryNone;
}
Kilderkin answered 21/11, 2008 at 6:58 Comment(1)
While compiling under iOS 4.x the following warning is received: WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWithIndexPath: in <MyViewController: 0x6e4fca0>. Please remove your implementation of this method and set the cell properties accessoryType and/or editingAccessoryType to move to the new cell layout behavior. This method will no longer be called in a future release. FYI.Injure
R
24

Jeff Lamarche has a tutorial on how to do this here:

http://iphonedevelopment.blogspot.com/2008/10/table-view-multi-row-edit-mode.html

I've not tried the code but it's been on the back of my mind for a while, knowing the day will come when I need it.

Recollected answered 24/3, 2009 at 5:4 Comment(0)
E
11

I backported allowsMultipleSelectionDuringEditing and allowsMultipleSelection from iOS5 to older iOS. You can fork it at https://github.com/ud7/UDTableView-allowsMultipleSelection

It's drop in replacement and only thing you need to do is change UITableView to UDTableView (in code or interface builder)

Ella answered 4/12, 2011 at 5:15 Comment(0)
R
5

From the HIG:

Table views provide feedback when users select list items. Specifically, when an item can be selected, the row containing the item highlights briefly when a user selects it to show that the selection has been received. Then, an immediate action occurs: Either a new view is revealed or the row displays a checkmark to indicate that the item has been selected. The row never remains highlighted, because table views do not display a persistent selected state.

You'll need to roll your own multiple selection style, either with something like Mail, or using the checkmark accessory on your cells.

React answered 21/11, 2008 at 12:1 Comment(0)
Y
5

Guys for multiple selection you just need

self.tableView.allowsMultipleSelection = YES;

on viewDidLoad and

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryView.hidden = NO; 
    // if you don't use custom image tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    tableViewCell.accessoryView.hidden = YES;
    // if you don't use custom image tableViewCell.accessoryType = UITableViewCellAccessoryNone;
}
Yeta answered 11/12, 2012 at 20:31 Comment(0)
M
3

I was searching for the same issue and the answer of Bhavin Chitroda sovled it for me but with some addition to keep the check mark as it was while scrolling.

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

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if ( [array indexOfObject:indexPath] == NSNotFound ) {
            [array addObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else {
            [array removeObject:indexPath];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

}

The addition:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
// Your code here
.
.
.
    if ( [array indexOfObject:indexPath] == NSNotFound ) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    return cell;

}
Millard answered 16/1, 2013 at 10:8 Comment(0)
G
2

If you're trying to do something like Mail's multiple-select (to delete mail, for example), then you're probably going to have to manage all the selection yourself. Multiple row selection isn't something that's standard on the iPhone. Mail solves this by using checkmarks to indicate which rows have been selected.

Galwegian answered 21/11, 2008 at 11:46 Comment(0)
C
2

blue highlighted row as an indicator of whether a row is selected is actually discouraged according to the HIG page 121. Checkmarks will do the trick.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    int selectedRow = indexPath.row;
    cout << "selected Row: " << selectedRow << endl;
    UITableViewCell *indexPathForCell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPathForCell.accessoryType == UITableViewCellAccessoryNone) {
        indexPathForCell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        indexPathForCell.accessoryType = UITableViewCellAccessoryNone;
    }

}

then add your arraying or how ever you wish to store the data of which were selected.

Chicken answered 29/2, 2012 at 19:1 Comment(0)
P
1

Note: This does not work in iOS 4+. This is a private, undocumented constant. Do not use it.

If you are not planning to submit your app to the App Store, you can invoke multi-row edit mode by implementing the following method in your UITableViewController delegate:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 3; // Undocumented constant
}
Pearle answered 6/8, 2010 at 21:30 Comment(4)
This works beautifully! Why oh why isn't this documented and allowed?!! Has anyone gotten an app approved with this in it? Not calling any undocumented methods... ?Jazmin
Yeah what a bummer that this is still a private API. So SO useful.Tenstrike
apparently it is not anymore on 4.x---Strangles
To clarify: this doesn't seem to work anymore under iOS4 from my testing (I wasn't sure if Digital Robot was saying that it wasn't private or had been disabled)Inflate
N
0

Tested with iOS4.3 - 6.0

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller {

    if ([controller.searchResultsTableView respondsToSelector:@selector(allowsMultipleSelectionDuringEditing)]) {
        controller.searchResultsTableView.allowsMultipleSelectionDuringEditing = YES;
    }
    else {
        controller.searchResultsTableView.allowsSelectionDuringEditing = YES;
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellAccessoryCheckmark;
}
Nephralgia answered 12/11, 2012 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.