Reputation: 991
Here is the Video showing problem in action: http://www.youtube.com/watch?v=vWznBUsppRg
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.
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.
top of .m:
static NSUserDefaults *prefs = nil;
static UISwitch *switchView = nil;
static NSMutableArray *array = nil;
-viewDidLoad I'm using -
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
}
-numberOfRowsInSection i have:
if (section == 3){
return [array count];
}
return section;
-cellForRowAtIndexPath i have:
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;
And Finally The UISwitch switchChanged:
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];
}
I've looked at almost every post concerning deleteRowsAtIndexPaths & insertRowsAtIndexPaths on Stack but none have a good amount of examples to work on/around.
Upvotes: 0
Views: 514
Reputation: 9915
Your data model (array) is getting corrupted by the following line when the switch is turned on:
[array addObjectsFromArray:indexPaths];
I think what you meant is:
[array insertObject:@"Video Quality" atIndex:indexPath.row];
If that's not it, it would help if you could explicitly state what the intended behavior is.
Upvotes: 2