tableView 最终崩溃,超过 16 个项目 [英] tableView crashes on end up with more than 16 items

查看:13
本文介绍了tableView 最终崩溃,超过 16 个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很令人困惑.

我有一个 UITableView,它会更新并正常工作,直到它获得超过 16 个项目,然后在调用 insertRowsAtIndexPaths 后尝试 endUpdates 时崩溃>.

I have a UITableView, which updates and works fine until it gets more than 16 items then it crashes when trying to endUpdates after calling insertRowsAtIndexPaths.

被添加的 NSIndexPath 都是有效的.-numberOfRowsInSection 返回正确的数字.它不会引发与数据集相关的错误,而是会崩溃

The NSIndexPaths being added are all valid. -numberOfRowsInSection returns the correct number. It is not throwing an error related to the data set, rather it crashes with

*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

tableView 上调用 endUpdates 时.

数据源都在那里,NSIndexPath 没问题.该代码在 0 到 16 行之间运行良好,但是当我添加第 17 行时它会崩溃.此外,如果我从 22 个项目开始它工作正常,当我添加第 23 个项目时它会崩溃......如果我调用重新加载数据而不是执行更新和插入过程它工作正常,所以它与数据本身无关,并且它应该与我插入行的方式无关,因为它可以工作到 16...

The data source is all there, the NSIndexPaths are fine. the code works fine between 0 and 16 rows, but when I add a 17th it crashes. Additionally if I start it with 22 items it works fine, when I add the 23rd it crashes... if I call reload data instead of doing the update and insert process it works fine, so it's nothing to do with the data itself, and it shouldn't be anything to do with how I'm inserting the rows since it works through 16...

我完全困惑了.这是我的更新方法.它一直在主线程上被调用.

I'm completely perplexed. Here is my update method. It is being called on the main thread at all times.

- (void)updateConversation:(NSNotification*)notification
{
    NSDictionary *updateInfo = [notification userInfo];
    //NSLog(@"Got update %@", updateInfo);

    if ([[updateInfo objectForKey:@"success"] integerValue] == YES) {
        [self updateConversationUI];

        int addedStatementCount = [[updateInfo objectForKey:@"addedStatementCount"] intValue];

        if (addedStatementCount > 0) {
            //[self.tableView reloadData];
            [self.tableView beginUpdates];
            int previousStatmentCount = [[updateInfo objectForKey:@"previousStatmentCount"] intValue];

            NSLog(@"owner %i, Was %i, now %i, change of %i", self.owner, previousStatmentCount, (int)self.conversation.statements.count, addedStatementCount);

            NSMutableArray *rowPaths = [[NSMutableArray alloc] init];

            for (int i = previousStatmentCount; i < previousStatmentCount + addedStatementCount; i++) {
                NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
                [rowPaths addObject:path];
            }

            [self.tableView insertRowsAtIndexPaths:rowPaths withRowAnimation:UITableViewRowAnimationBottom];        
            [self.tableView endUpdates];
            [self.tableView scrollToRowAtIndexPath:[rowPaths lastObject] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }
}

过去 [self.tableView endUpdates] 崩溃的其余部分是 UITableView

The rest of the crash past [self.tableView endUpdates] is UITableView

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010189b795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001015fe991 objc_exception_throw + 43
    2   CoreFoundation                      0x0000000101852564 -[__NSArrayM insertObject:atIndex:] + 820
    3   UIKit                               0x0000000100317900 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke691 + 173
    4   UIKit                               0x00000001002b5daf +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 460
    5   UIKit                               0x00000001002b6004 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 57
    6   UIKit                               0x00000001003174cb -[UITableView _updateWithItems:updateSupport:] + 2632
    7   UIKit                               0x0000000100312b18 -[UITableView _endCellAnimationsWithContext:] + 11615
    8   Dev App                         0x0000000100006036 -[ConversationViewController updateConversation:] + 998
    9   CoreFoundation                      0x00000001018f121c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    10  CoreFoundation                      0x000000010185370d _CFXNotificationPost + 2381
    11  Dev App                         0x00000001000055ac -[ConversationManager postNotification:] + 92
    12  Foundation                          0x0000000101204557 __NSThreadPerformPerform + 227
    13  CoreFoundation                      0x000000010182aec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x000000010182a792 __CFRunLoopDoSources0 + 242
    15  CoreFoundation                      0x000000010184661f __CFRunLoopRun + 767
    16  CoreFoundation                      0x0000000101845f33 CFRunLoopRunSpecific + 467
    17  GraphicsServices                    0x00000001039a23a0 GSEventRunModal + 161
    18  UIKit                               0x0000000100261043 UIApplicationMain + 1010
    19  Dev App                         0x0000000100003613 main + 115
    20  libdyld.dylib                       0x0000000101f2a5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这似乎是操作系统中的一个错误,堆栈会表明它与动画块有关,但无论我将动画设置为什么都会发生,包括没有.

This seems like a bug in the OS, the stack would indicate that it's something to do with the animation block but it happens no matter what I set the animation to, including none.

并重新声明.这适用于 16 个项目,然后插入导致崩溃的项目.它还在初始设置时被调用以加载数据,它会在那里加载任意数量的项目,包括超过 16 个.但是当再次调用时,纯粹作为从 16 或更多更新到更高的项目时,它会崩溃.

And to re-state. This works through 16 items, then inserting items past that causes a crash. It is also called at initial setup to load data, and it will load any number of items there, including well over 16. But when called again, purely as an update from 16 or more to something higher it will crash.

我还没有遇到过关于表格视图的最奇怪的问题...

Most bizarre issue I've yet to encounter with table views...

iOS 7下的设备和模拟器,最新的Xcode供参考.

under iOS 7 on device and simulator, latest Xcode for reference.

推荐答案

看来问题是由我调用 scrollToRowAtIndexPath 引起的(即使它在到达那里之前崩溃了......)结合实现 tableView:estimatedHeightForRowAtIndexPath: by删除行高估计崩溃消失了……对我来说,对于表格来说,这似乎仍然是动画系统中的一个错误.谢天谢地,我不需要估计的行高,我忘记了我已经实现了它(试图在 iOS 7 上玩得很好又咬了我一口).

It seems that the problem is caused by my call of scrollToRowAtIndexPath (even though it crashes before it gets there...) combined with implementing tableView:estimatedHeightForRowAtIndexPath: by removing the row height estimate the crash went away... still seems like a bug in the animation system for tables to me. Thankfully I don't need the estimated row height, I had forgotten I had implemented it (trying to play nice by iOS 7 bites me again).

这篇关于tableView 最终崩溃,超过 16 个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆