我几天来一直在努力解决这个问题.我需要在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 == 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
Run Code Online (Sandbox Code Playgroud)
当我运行它并点击任何行时,我得到以下异常:
*** 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.
Run Code Online (Sandbox Code Playgroud)
这段代码有什么根本原因我没看到吗?
Ole*_*ann 11
该文档insertRowsAtIndexPaths:withRowAnimation:明确说明在更新块中插入之前删除:
请注意在beginUpdates和endUpdates方法定义的动画块中调用此方法时的行为.UITableView延迟任何行或节的插入,直到它处理了行或节的删除.无论插入和删除方法调用的顺序如何,都会发生这种情况.
"表视图编程指南"中的更多详细信息:
但是,这不是UITableView完成操作的顺序.在处理行或节的删除之前,它会延迟任何行或节的插入.表视图的行为与更新块内部调用的重新加载方法的行为相同 - 在执行动画块之前,相对于行和段的索引进行重新加载.无论插入,删除和重新加载方法调用的顺序如何,都会发生此行为.
动画块中的删除和重新加载操作指定应删除或重新加载原始表中的哪些行和部分; insertions指定应将哪些行和部分添加到结果表中.用于标识节和行的索引路径遵循此模型.另一方面,在可变数组中插入或移除项目可能会影响用于连续插入或移除操作的数组索引; 例如,如果在某个索引处插入项,则数组中所有后续项的索引都会递增.
| 归档时间: |
|
| 查看次数: |
5423 次 |
| 最近记录: |