views:

7306

answers:

6

Hey there,

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.

+1  A: 

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.

August
+1  A: 

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.

Ben Gottlieb
+7  A: 

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

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

Do 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.

z8000
+5  A: 

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;
}
Benjamin Ortuzar
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.
DenTheMan
+10  A: 

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.

oldbeamer
A: 

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
}
titaniumdecoy