Reputation: 1388
I'm trying to have an edit button select all the cells on a table. I've been trying to figure out what I am doing wrong. Here is my code. I am basically looping through the sections and then through the number of rows. Whatever I do, it does not setSelect each cell. How would I achieve this?
@property (strong, nonatomic) IBOutlet UITableView *caseDataTableView;
@synthesize segmentControl,caseDataTableView,IndicationView,procedureView,indicationScrollView,procedureScrollView,segmentControllView,segmentControllios6;
- (IBAction)editButtonTapped:(id)sender {
for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) {
for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) {
UITableViewCell *cell = [self.caseDataTableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:r inSection:i]];
[cell setSelected:YES];
}
}
}
Upvotes: 3
Views: 1050
Reputation: 1388
The problem that I had was two things. The first being the editButtonTapped method being incorrect.
- (IBAction)editButtonTapped:(id)sender {
for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) {
for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) {
[self tableView:caseDataTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:i]];
}
}}
The next issue was the didSelectRowAtIndexPath method scrolling down when I would do my loop. It would eventually go down to the last cell in the previous loop. I took this piece of code out on the didSelectRowAtIndexPath method.
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]
Setting the scrollPosition in my situation would clear the selections out and thats why it was removed.
Upvotes: 0
Reputation: 2537
You can use a for loop
- (IBAction)editButtonTapped:(id)sender
{
for (int i = 0; i < self.tableView.numberOfSections; i++)
{
for (int j = 0; j < [self.tableView numberOfRowsInSection:i]; j++)
{
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i]
animated:NO
scrollPosition:UITableViewScrollPositionNone];
}
}
}
Upvotes: 2
Reputation: 1544
You can select a cell calling table view's selectRowAtIndexPath method:
[caseDataTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
Upvotes: 0