Я боролся с этой проблемой уже пару дней.Мне нужно одновременно удалить и вставить несколько строк в UITableView внутри блока beginUpdates / endUpdates, но некоторые из вставленных строк имеют индекс, который превосходит исходное количество элементов табличного представления.Я взял пример кода из документации Apple по пакетной вставке и удалению и изменил его, чтобы вызвать ту же проблему, вот код:
#import "MasterViewController.h"
@implementation MasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Master", @"Master");
states = [[NSMutableArray arrayWithObjects:@"Arizona", @"California", @"New Jersey", @"Washington", nil] retain];
}
return self;
}
- (void)dealloc
{
[states release];
[super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [states count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [states objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[states removeObjectAtIndex:0];
[states removeObjectAtIndex:0];
[states removeObjectAtIndex:0];
[states insertObject:@"Alaska" atIndex:1];
[states insertObject:@"Georgia" atIndex:1];
[states insertObject:@"Wisconsin" atIndex:1];
NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
nil];
NSArray *insertIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0],
[NSIndexPath indexPathForRow:4 inSection:0],
nil];
UITableView *tv = (UITableView *)self.view;
[tv beginUpdates];
[tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
[tv deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationTop];
[tv endUpdates];
}
@end
Когда я запускаю это и нажимаю на любую строку, я получаю следующееисключение:
*** Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableViewSupport.m:386
Uncaught exception: Invalid table view update. The application has requested an update to the table view that is inconsistent with the state provided by the data source.
Что-то в корне не так с этим кодом, которого я не вижу?