tags:

views:

410

answers:

2

what would be the best method to select all the cells in the table(UITableView) when the user presses a button in the tool bar?

+1  A: 

You can select a cell calling table view's selectRowAtIndexPath method:

[menuTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];

However you can't select multiple cells in a UITableView. If you want to show and process on/off state of cells you should use cell with accessory view with UITableViewCellAccessoryCheckmark type. (see docs for more details)

Vladimir
A: 

Haven't tried it myself, but try this:

In your button action, loop through the indexPath and call it:

for (i = 0; i < [tableView numberOfSections]; i++) {
    for (j = 0; j < [tableView numberOfRowsInSection:i]; j++) {
         indexPath.row = j;
         indexPath.section = i;
         [tableView selectRowAtIndexPath:indexPath animated:animated scrollPosition:scrollPosition];
    }
}
mahboudz
When you select each new cell - selection from previous removed. You can't get multiple selection in UITableView it seems
Vladimir
I wonder if you can reject the deselection in the tableView:willDeselectRowAtIndexPath: delegate method. (Return nil if you don’t want the row deselected.)
mahboudz
I does not work for either. Anyway the main point (imo, but can't find an exact place in HIG) is just that you should not select more then one cell at a time and selection state must not be persistent - you select table cell to do something. You should use cell's checkmark accessory view for changing/showing cell state.
Vladimir
I guess the solution then is to persist each selection internally (as what you say in your response) and color the cell accordingly to indicate a different type of selection (or use the check mark).
mahboudz
A similar discussion: http://stackoverflow.com/questions/1526586/
mahboudz