Reputation: 1255
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?
Upvotes: 14
Views: 17515
Reputation: 669
This can select all rows in the table view:
for (NSInteger s = 0; s < self.tableView.numberOfSections; s++) {
for (NSInteger r = 0; r < [self.tableView numberOfRowsInSection:s]; r++) {
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]
animated:NO
scrollPosition:UITableViewScrollPositionNone];
}
}
Upvotes: 19
Reputation: 299
Here is my method to traverse a table, it works well.
for (int i = 0; i < [ptableView numberOfSections]; i++) {
for (int j = 0; j < [ptableView numberOfRowsInSection:i]; j++) {
NSUInteger ints[2] = {i,j};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
UITableViewCell *cell = [ptableView cellForRowAtIndexPath:indexPath];
//Here is your code
}
}
Upvotes: 29
Reputation: 39376
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];
}
}
Upvotes: 3
Reputation: 170829
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)
Upvotes: 8