Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Add/Remove UITableViewCell when UISwitch is activated?
    text
    copied!<p>Here is the Video showing problem in action: <a href="http://www.youtube.com/watch?v=vWznBUsppRg" rel="nofollow">http://www.youtube.com/watch?v=vWznBUsppRg</a></p> <p>I'm getting weird behavior with my Array when adding a cell to a section. I'm developing a mobilesubstrate tweak..this is why I didn't post entire methods.</p> <p>The Cell is getting added to the bottom of the section when turned ON. But when turned off, the cell under the UISwitch cell gets removed like intended but screws up indexPaths.</p> <p>top of .m:</p> <pre><code>static NSUserDefaults *prefs = nil; static UISwitch *switchView = nil; static NSMutableArray *array = nil; </code></pre> <p>-viewDidLoad I'm using -</p> <pre><code>BOOL state = [prefs boolForKey:@"betaVid"]; array = [[NSMutableArray alloc] initWithObjects:@"Show Cached Videos", @"(Beta) Send Videos", @"Video Quality", @"Visit Website", @"Made by: @CokePokes", nil]; if (state == FALSE){ [array removeObject:@"Video Quality"]; //remove from array } </code></pre> <p>-numberOfRowsInSection i have:</p> <pre><code>if (section == 3){ return [array count]; } return section; </code></pre> <p>-cellForRowAtIndexPath i have:</p> <pre><code>prefs = [NSUserDefaults standardUserDefaults]; BOOL state = [prefs boolForKey:@"betaVid"]; if (indexPath.section == 3){ static NSString *CellIdentifier = @"Cell"; //NSString *CellIdentifier = [NSString stringWithFormat:@"Cell_%d",indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell... cell.textLabel.text = [NSString stringWithFormat:@"%@", [array objectAtIndex:indexPath.row]]; NSInteger counted = [array count]; NSLog(@"~~~~~~~~~~~~~~~~~~~hmmmmm.... Really is: %d", counted); if (counted == 4){ NSLog(@"~~~~~~~~~~~~~~~~~~~Array count is _4_. Really is: %d", counted); if (indexPath.row == 0){ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.textAlignment = UITextAlignmentLeft; } else if (indexPath.row == 1){ switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; cell.accessoryView = switchView; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; switchView.on = [prefs boolForKey:@"betaVid"]; [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; } else if (indexPath.row == 2){ //set up cell appearance..not important now } else if (indexPath.row == 3){ //set up cell appearance..not important now } } else if (counted == 5){ NSLog(@"~~~~~~~~~~~~~~~~~~~~Array count is _5_. Really is: %d", counted); if (indexPath.row == 0){ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.textAlignment = UITextAlignmentLeft; } else if (indexPath.row == 1){ switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; cell.accessoryView = switchView; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; switchView.on = [prefs boolForKey:@"betaVid"]; [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; } else if (indexPath.row == 2){ //set up cell appearance..not important now } return cell; } return indexPath; </code></pre> <p>And Finally The UISwitch switchChanged:</p> <pre><code>switchView = sender; NSLog( @"The switch is %@", switchView.on ? @"ON" : @"OFF" ); prefs = [NSUserDefaults standardUserDefaults]; [prefs setBool:switchView.on forKey:@"betaVid"]; [prefs synchronize]; BOOL state = [prefs boolForKey:@"betaVid"]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:3]; NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; if (state == TRUE) { [self.table beginUpdates]; [array addObjectsFromArray:indexPaths]; [self.table insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [self.table endUpdates]; [self.table reloadData]; } else if (state == FALSE){ [self.table beginUpdates]; [array removeObjectAtIndex:indexPath.row]; [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [self.table endUpdates]; [self.table reloadData]; } </code></pre> <p>I've looked at almost every post concerning deleteRowsAtIndexPaths &amp; insertRowsAtIndexPaths on Stack but none have a good amount of examples to work on/around.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload