我几天来一直在努力解决这个问题.我需要在beginUpdates/endUpdates块内的UITableView中同时删除和插入一些行,但是一些插入的行的索引超过了原始的表视图元素数.我从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 …Run Code Online (Sandbox Code Playgroud)