xcode -[UITableView _endCellAnimationsWithContext 中的断言失败:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26033504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:41:38  来源:igfitidea点击:

Assertion failure in -[UITableView _endCellAnimationsWithContext:

iosobjective-carraysxcodeuitableview

提问by Jayrod7387

I'm an amateur at best, and stuck on this error! surely something simple...

我充其量只是一个业余爱好者,并坚持这个错误!肯定是一些简单的事情......

- (void)addTapped:(id)sender {
TechToolboxDoc *newDoc = [[TechToolboxDoc alloc] initWithTitle:@"New Item" thumbImage:nil fullImage:nil];
[_itemArray addObject:newDoc];
//[self.tableView beginUpdates];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_itemArray.count-1 inSection:0];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
NSLog(@"%@",indexPath);

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES];

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"MySegue" sender:self];

   //[self.tableView endUpdates];

it is breaking on the line the says

它正在断线上说

 [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES];

回答by Droppy

You need to add [UITableView beginUpdates]and [UITableView endUpdates]around:

您需要添加[UITableView beginUpdates][UITableView endUpdates]周围:

[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:YES];

From the class reference:

从类参考:

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. This is unlike inserting or removing an item in a mutable array, where the operation can affect the array index used for the successive insertion or removal operation. For more on this subject, see Batch Insertion, Deletion, and Reloading of Rows and Sections in Table View Programming Guide for iOS.

请注意在由 beginUpdates 和 endUpdates 方法定义的动画块中调用此方法时的行为。UITableView 将任何行或节的插入推迟到它处理完行或节的删除之后。无论插入和删除方法调用的顺序如何,都会发生这种情况。这与在可变数组中插入或删除项目不同,在可变数组中,该操作会影响用于连续插入或删除操作的数组索引。有关此主题的更多信息,请参阅 iOS 表视图编程指南中的行和节的批量插入、删除和重新加载。

回答by iHulk

I think you are inserting the row in your tableViewbut not updating the number of rows at sectionthat's why you are getting error in this line. So along with inserting the row You should also increase the array count or whatever you are using to show the number of rows in table view.

我认为你是在你的行中插入行,tableView但没有更新,number of rows at section这就是你在这一行中出错的原因。因此,在插入行的同时,您还应该增加数组计数或用于在表视图中显示行数的任何内容。

回答by Lukesivi

In my case, I was using Parse and I deleted a few users (one of which was my iPhone simulator). I got this error whenever refreshing the tableView.

就我而言,我使用 Parse 并删除了一些用户(其中​​一个是我的 iPhone 模拟器)。每当刷新 tableView 时,我都会收到此错误。

I just deleted the app from the Simulator and made a new account and it works flawlessly.

我刚刚从模拟器中删除了该应用程序并创建了一个新帐户,它可以完美运行。

@droppy's answer helped give me the lightbulb moment of what was wrong!

@droppy 的回答让我知道出了什么问题!

Hope that helps someone.

希望能帮助某人。