ios -[UITableView _endCellAnimationsWithContext:] 中的断言失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10134841/
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-08-30 17:50:13  来源:igfitidea点击:

Assertion failure in -[UITableView _endCellAnimationsWithContext:]

objective-ciosuitableviewcrash

提问by Scubadivingfool

Hopefully this will be a quick fix. I have been trying to figure out the error that i keep getting. The error is listed below and the appdelagate is below that.

希望这将是一个快速修复。我一直在试图找出我不断收到的错误。错误在下面列出,appdelagate 在下面。

Any help is appreciated.

任何帮助表示赞赏。

Thanks

谢谢

2012-04-12 21:11:52.669 Chanda[75100:f803] --- Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:10372012-04-12 21:11:52.671 Chanda[75100:f803] --- Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

2012-04-12 21:11:52.669安达[75100:F803] ---在声明失败-[UITableView _endCellAnimationsWithContext:]/SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:10372012-04-12 21:11:52.671安达[75100:F803] ---终止应用程序由于未捕获的异常' NSInternalInconsistencyException' ,原因:'无效更新:第0节中的行数无效。更新(2)后现有节中包含的行数必须等于更新前该节中包含的行数(2),加上或减去从该部分插入或删除的行数(插入 1 行,删除 0 行),加上或减去移入或移出该部分的行数(0 移入,0 移出)。

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize databaseName,databasePath; 

- (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
    self.databaseName = @"Customers.db";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    self.databasePath = [documentDir stringByAppendingPathComponent:self.databaseName];
    [self createAndCheckDatabase];

    return YES;
}

- (void)createAndCheckDatabase {
    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];

    if (success) return; 

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseName];

    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

@end

回答by pbx

I don't see the reason for you to show us this part of code. Your error must be connected to this part in your code I assume

我看不出你有什么理由向我们展示这部分代码。您的错误必须与我假设的代码中的这一部分有关

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Probably you are making a mistake in one of these data source methods. Currently it's impossible to say what exactly is wrong but I assume it could be something like: You are telling the table view in the numberOfRowsInSectionyou would like to have nrows reserved and setup and in the cellForRowAtIndexPathyou then only handle n - 1rows for example.

您可能在这些数据源方法之一中犯了错误。目前无法说出究竟出了什么问题,但我认为它可能是这样的:例如,您告诉表视图numberOfRowsInSection您希望保留和设置n行,cellForRowAtIndexPath然后仅处理n - 1行。

Sorry that this answer can't be as precise as it should be. If you show us your implementation of your data source it would be much easier to tell what's going on.

对不起,这个答案不能像它应该的那样精确。如果您向我们展示您的数据源的实现,则更容易了解发生了什么。

回答by abbood

Like Sun Tzu said: it's best to win without fighting. In my case whenever I see this kind of error message (ie discrepancy between rows added deleted etc).. I don't even debug anything.. I simply avoid making that extra call where I reload the rows etc.. that's 99% of the cases where this error happens.

就像孙子说的不战而胜。在我的情况下,每当我看到这种错误消息(即添加的行之间的差异被删除等)..我什至不调试任何东西..我只是避免在重新加载行等的地方进行额外的调用..那是 99%发生此错误的情况。

This is a common scenario where this bug happens: I have a UINavigationControllerand it has a UITableView, when I click on a row it pushes a new UITableViewand so on. This error always happens to me when I pop the last UITableviewand go back to the UITableViewbefore it, at this point I make an unnecessary call to the loadItfunction which basically inserts the rows and relaods the UITableView.

这是发生此错误的常见情况:我有一个UINavigationController并且它有一个UITableView,当我单击一行时它会推送一个新的UITableView等等。当我弹出最后一个UITableview并返回到它UITableView之前时,这个错误总是发生在我身上,此时我对loadIt函数进行了不必要的调用,该函数基本上插入行并重新加载UITableView.

The reason this happens is because I erroneously place my loadIt function in viewDidAppear:animatedrather than viewDidLoad. viewDidAppear:animatedis called every time the UITableViewis displayed, viewDidLoadis only called once.

发生这种情况的原因是我错误地将 loadIt 函数放在viewDidAppear:animated而不是viewDidLoad. viewDidAppear:animated每次UITableView显示时viewDidLoad调用,只调用一次。

回答by Olof_t

When removing rows, remember that it also checks sections when updating, in:

删除行时,请记住它还会在更新时检查部分,在:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView

If you want to remove a row that is the last item in a section you need to remove the whole section instead (otherwise it might get section count wrong and throw this exception).

如果要删除作为节中最后一项的行,则需要删除整个节(否则可能会导致节计数错误并引发此异常)。

回答by love2script12

Don't forget to update your array which determines numberOfRowsInSection. It needs to be updated before you animate and remove

不要忘记更新确定 numberOfRowsInSection 的数组。它需要在您制作动画和删除之前更新

We check if number of rows in section is 1 because we will have to delete the entire section.

我们检查部分中的行数是否为 1,因为我们将不得不删除整个部分。

Do correct me if anyone can make this answer clearer.

如果有人能让这个答案更清楚,请纠正我。

[self.tableView beginUpdates];
if ([tableView numberOfRowsInSection:indexPath.section] == 1) {

   [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
} else {

   [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
[self.tableView endUpdates];

回答by Bob

I put each section elements in separated arrays. Then put them into another array( arrayWithArray). My solution here for this problem:

我将每个部分元素放在单独的数组中。然后将它们放入另一个数组(arrayWithArray)中。我在这里针对这个问题的解决方案:

[quarantineMessages removeObject : message];
[_tableView beginUpdates];
if([[arrayWithArray objectAtIndex: indPath.section] count]  > 1)
{
    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indPath] withRowAnimation:UITableViewRowAnimationBottom];
}
else
{
    [_tableView deleteSections:[NSIndexSet indexSetWithIndex:indPath.section]
              withRowAnimation:UITableViewRowAnimationFade];
}
[_tableView endUpdates];

回答by user1612868

I had the same error.

我有同样的错误。

I was using the following lines

我正在使用以下几行

UINib *myCustomCellNib = [UINib nibWithNibName:@"CustomNib" bundle:nil];
[tableView registerNib:myCustomCellNib forCellReuseIdentifier:@"CustomNib"];

to register the nib in the viewDidLoad method, since I had a different nib that was also associated with the same class. Hence, the line

在 viewDidLoad 方法中注册笔尖,因为我有一个不同的笔尖,它也与同一个类相关联。因此,该行

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GBFBLoadingCell"];

was returning nil unless I registered the nib in the viewDidLoad.

除非我在 viewDidLoad 中注册了笔尖,否则返回 nil。

My problem was that I forgot to set the identifier in the attributes inspector for my file "CustomNib.xib" and "CustomNib~iphone.xib". (Or more precisely, that I forgot to press enter after typing the identifier in the attribute inspector in XCode, so that the new name failed to save.)

我的问题是我忘记在属性检查器中为我的文件“CustomNib.xib”和“CustomNib~iphone.xib”设置标识符。(或者更准确地说,我在 XCode 的属性检查器中输入标识符后忘记按 Enter 键,因此无法保存新名称。)

Hope this helps.

希望这可以帮助。

回答by mikeho

If you're using an NSFetchedResultsControllerlike me and updating data in a background thread, don't forget to begin and end updates in the delegate:

如果您使用NSFetchedResultsController像我这样的工具并在后台线程中更新数据,请不要忘记在委托中开始和结束更新:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

回答by user5553647

I Had the same error , which when trying with [tableView reloadData]was working fine . The error was actually in the line

我有同样的错误,在尝试[tableView reloadData]时工作正常。错误实际上是在行中

[TabView insertRowsAtIndexPaths:indexPathsArray withRowAnimation:UITableViewRowAnimationRight];

When i tried to check the indexPath values , they weren't correct as required .

当我尝试检查 indexPath 值时,它们没有按要求正确。

I fixed it by changing values in indexPathsArray.

我通过更改indexPathsArray.

Hope this helps .

希望这可以帮助 。

回答by Ted

Its could be one of UITableViewDataSourceprotocol methods

它可能是UITableViewDataSource协议方法之一

For

为了

- tableView:numberOfRowsInSection:

it should return an integer equal to the sum or result of

它应该返回一个等于总和或结果的整数

-insertRowsAtIndexPaths:withRowAnimation:and/or -deleteRowsAtIndexPaths:withRowAnimation:

-insertRowsAtIndexPaths:withRowAnimation:和/或-deleteRowsAtIndexPaths:withRowAnimation

For

为了

- numberOfSectionsInTableView:

it should return an integer equal to the sum or result of

它应该返回一个等于总和或结果的整数

-insertRowsAtIndexPaths:withRowAnimation:and/or -deleteSections:withRowAnimation:

-insertRowsAtIndexPaths:withRowAnimation:和/或 -deleteSections:withRowAnimation:

回答by Evgeniy Kleban

Simply check that you call [yourTableView reloadData]; after modify array of values.

只需检查您是否调用了 [yourTableView reloadData]; 修改值数组后。