I have a UITableViewController that has a tableView. Each cell has a button that can be selected/deslected like a checkbox, and the method that does this is working fine. I would like to have another method that gets called by a button in the tableheaderview that is able to iterate through this list and select all of the buttons in one fell swoop. The irony is that I am able to perform the iteration, but can’t seem to call the method that selects/deselects the button.
Here is my relevant code:
//This is my method does the iteration
- (void) startWizard {
NSLog(@"Did it select?");
for (int i = 0; i < [self.tableView numberOfSections]; i++) {
for (int j = 0; j < [self.tableView numberOfRowsInSection:i]; j++) {
NSUInteger ints[2] = {i,j};
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Here is my code to call the method that selects/deselects
[self buttonTouched:nil];
}
}
}
and here is the method that does the actual select/deselect of each individual button (and works fine):
-(void)buttonTouched:(id)sender
{
UIButton *btn = (UIButton *)sender;
if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"CheckBox1.png"]])
{
[btn setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
// other statements
}
else
{
[btn setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateNormal];
// other statements
}
}
The button in each cell has been added to that cell as its accesseryView as follows:
[cell setAccessoryView:testButton];
Can anyone see what I am doing wrong, and how I can fix this?
When you call in the loop
[self buttonTouched:nil];you call the methodbuttonTouchedwithnilas value forsender. Therefore, the first line inbuttonTouchedsetsbtntoniland the rest of the methods operates on the nil button.Either find a way to pass the actual button object to
buttonTouchedor pass the indexPath an d find the correct button by that.