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];
}
}
}
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];
}
}
}
您可以选择一个单元格调用表视图的selectRowAtIndexPath方法:
[caseDataTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.