I have a tableview each cell contains a text and a button. For each button in each cell in cellForRowAtIndexPath I define an action using a selector as such:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProductCell" forIndexPath:indexPath];
cell.stateButton.tag = indexPath.row;
[cell.stateButton addTarget:self action:@selector(stateChange:) forControlEvents:UIControlEventTouchUpInside];
/// Rest of code
return cell;
}
At some point in the flow I want to generate an action for all the buttons in my tableview. I do it like that:
#pragma mark - Delegate
- (void)onAllSwitchesStateChange{
for(int i = 0; i < [[SsrDeviceManager instance] ssrDevices].count; i ++){
ProductTableViewCell* cell = (ProductTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
UIButton* stateButton = [cell stateButton];
[stateButton performSelector:@selector(stateChange:) withObject:nil];
}
}
This is my selector method:
-(void)stateChange:(UIButton*)sender{
[self.activityIndicator sizeToFit];
[sender addSubview:self.activityIndicator];
[self.activityIndicator startAnimating];
//// Rest of code
}
However, why I try to run this code, I get an error and the app crashes with this message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton stateChange:]: unrecognized selector sent to instance 0x1023686d0'
*** First throw call stack:
(0x233133180 0x23230b9f8 0x23304f9bc 0x25f94c220 0x2331389c8 0x23313a65c 0x101073bdc 0x100fdd844 0x25f922300 0x25f3cb424 0x25f3cb744 0x25f3ca7b0 0x25f9595c4 0x25f95a7ec 0x25f93a85c 0x25fa009d4 0x25fa03100 0x25f9fc330 0x2330c4f1c 0x2330c4e9c 0x2330c4784 0x2330bf6c0 0x2330befb4 0x2352c079c 0x25f920c38 0x10102b794 0x232b828e0)
How can I generate a click action for all buttons at once
The very notion makes no sense. A table view is just view. It lets the user communicate with you (the app) because it is touchable interface, but it is not, in itself, a source of activity; it merely represents the data.
If you want something to happen, it is insane to turn to all your buttons and ask them all to initiate some sort of action. Simply do to your data, yourself, whatever it is that you want done.