I had this problem on mine, and in order to avoid a crash, I should not explicitly use uiswitch, but instead pass the information to a logical one, this is how I did it.
Add a boolean to the top of the implementation file
bool _showRows = NO;
Update your uiswitch code
- (IBAction)SwitchDidChange:(id)sender { NSArray *aryTemp = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:1 inSection:0], [NSIndexPath indexPathForRow:2 inSection:0], [NSIndexPath indexPathForRow:3 inSection:0], [NSIndexPath indexPathForRow:4 inSection:0],nil]; if (_showRows) { _showRows = NO; _switch.on = NO; [_tblView deleteRowsAtIndexPaths:aryTemp withRowAnimation:UITableViewRowAnimationTop]; } else { _showRows = YES; _switch.on = YES; [_tblView insertRowsAtIndexPaths:aryTemp withRowAnimation:UITableViewRowAnimationBottom]; } }
Finally, update your NumberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { if (_showRows) { return 5; } else { return 1; } } return 0; }
source share