gpt4 book ai didi

ios - 在 UITableView 中同时插入/删除行的问题

转载 作者:行者123 更新时间:2023-11-28 18:00:37 26 4
gpt4 key购买 nike

这几天我一直在为这个问题苦苦挣扎。我需要在 beginUpdates/endUpdates block 内的 UITableView 中同时删除和插入一些行,但一些插入的行具有超过原始 TableView 元素计数的索引。我从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.

这段代码是否存在我没​​有发现的根本错误?

最佳答案

insertRowsAtIndexPaths:withRowAnimation: 的文档明确指出,删除先于插入更新 block :

Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods. UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls.

TableView 编程指南中的更多详细信息:

However, this is not the order in which UITableView completes the operations. It defers any insertions of rows or sections until after it has handled the deletions of rows or sections. The table view behaves the same way with reloading methods called inside an update block—the reload takes place with respect to the indexes of rows and sections before the animation block is executed. This behavior happens regardless of the ordering of the insertion, deletion, and reloading method calls.

Deletion and reloading operations within an animation block specify which rows and sections in the original table should be removed or reloaded; insertions specify which rows and sections should be added to the resulting table. The index paths used to identify sections and rows follow this model. Inserting or removing an item in a mutable array, on the other hand, may affect the array index used for the successive insertion or removal operation; for example, if you insert an item at a certain index, the indexes of all subsequent items in the array are incremented.

关于ios - 在 UITableView 中同时插入/删除行的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com