これが実際の問題を示すビデオです: http ://www.youtube.com/watch?v = vWznBUsppRg
セクションにセルを追加すると、配列で奇妙な動作が発生します。私はモバイルサブストレートの微調整を開発しています。これが、メソッド全体を投稿しなかった理由です。
オンにすると、セルがセクションの下部に追加されます。ただし、オフにすると、UISwitchセルの下のセルは意図したとおりに削除されますが、indexPathsが台無しになります。
.mの上部:
static NSUserDefaults *prefs = nil;
static UISwitch *switchView = nil;
static NSMutableArray *array = nil;
-私が使用しているviewDidLoad-
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私は持っています:
if (section == 3){
return [array count];
}
return section;
-cellForRowAtIndexPath私は持っています:
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;
そして最後に、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];
}
スタック上のdeleteRowsAtIndexPathsとinsertRowsAtIndexPathsに関するほぼすべての投稿を見てきましたが、作業する/回避するための十分な例はありません。