NSInternalInconsistencyException 原因:'无效更新:第 0 节中的行数无效'

Posted

技术标签:

【中文标题】NSInternalInconsistencyException 原因:\'无效更新:第 0 节中的行数无效\'【英文标题】:NSInternalInconsistencyException reason: 'Invalid update: invalid number of rows in section 0'NSInternalInconsistencyException 原因:'无效更新:第 0 节中的行数无效' 【发布时间】:2014-06-25 06:06:23 【问题描述】:

我有一个名为 dummyData 的数组数组,但在将数组 cmets 的计数作为表中的行数返回时遇到问题。

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
NSArray *commentsArray=[[dummyData objectAtIndex:section] valueForKeyPath:@"comments"];
NSLog(@"Comments: %d", [commentsArray count]);

if (section == didSection) 
    return [[[dummyData objectAtIndex:section] valueForKeyPath:@"comments"] count];

return 0;

它给了我这个错误:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(3)必须等于行数更新前包含在该节中的行数 (0),加上或减去从该节插入或删除的行数(2 插入,0 删除),加上或减去移入或移出该节的行数(0 移入, 0 移出)。'

这是我的数组:

dummyData = [[NSMutableArray alloc] init];

[dummyData addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", @"badge.png",@"Got Strike! at", @"Alex Shin",
                                                          [NSArray arrayWithObjects:
                                                           [NSDictionary dictionaryWithObjects:
                                                                [NSArray arrayWithObjects:@"James", @"30 minutes ago", @"Great Shot!", @"https://c676132.ssl.cf0.rackcdn.com/_84A2468-50cf361795eb2.jpg", nil]
                                                            forKeys:
                                                                [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Kim", @"10 minutes ago", @"I should have seen it.", @"http://3.bp.blogspot.com/-tBFQC9y-ZpM/Tmb8DaeYEmI/AAAAAAAAA9M/TahVx2IiQsE/s400/Bowling+and+Best+Bowling+Player+Shalin+Zulkifli+cover.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Matt", @"5 minutes ago", @"Nice", @"http://www.beverlyhillsmagazine.com/wp-content/uploads/Ashton-Kutcher-Hollywood-Stars-Rich-and-Famous-Movie-Stars-Star-of-the-Week-Beverly-Hills-Magazine-Hollywood-Magazines--300x336.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]]

                                                           , nil] , nil]

                     forKeys:[NSArray arrayWithObjects:@"photo", @"credit",@"credit_label", @"name", @"comments", nil]
                                                          ]];

[dummyData addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", @"location_icon.png",@"Checked in at", @"Jessah Ouano",
                                                          [NSArray arrayWithObjects:
                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Matt", @"1 hour ago", @"Welcome!", @"http://www.beverlyhillsmagazine.com/wp-content/uploads/Ashton-Kutcher-Hollywood-Stars-Rich-and-Famous-Movie-Stars-Star-of-the-Week-Beverly-Hills-Magazine-Hollywood-Magazines--300x336.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Steve", @"47 minutes ago", @"You'll enjoy here.", @"http://www.oozyleaks.com/wp-content/uploads/2013/09/Taylor-Lautner.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]]



                                                           , nil] , nil]

                                                 forKeys:[NSArray arrayWithObjects:@"photo", @"credit",@"credit_label", @"name", @"comments", nil]
                      ]];

[dummyData addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", @"location_icon.png",@"Checked in at", @"Richard Son",
                                                          [NSArray arrayWithObjects:
                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Pinky", @"3 hours ago", @"I saw it", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Tom", @"1 hour ago", @"realy?", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Caty", @"11 minutes ago", @"yes", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]]



                                                           , nil] , nil]

                                                 forKeys:[NSArray arrayWithObjects:@"photo", @"credit",@"credit_label", @"name", @"comments", nil]
                      ]];

[dummyData addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", @"badge.png",@"Got a Strike! at", @"Dexter Dulay",
                                                          [NSArray arrayWithObjects:
                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Matt", @"12 minutes ago", @"Take the opportunity", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Dex", @"11 minutes ago", @"Maybe later", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Drew", @"9 minutes ago", @"Alex should win", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Alex", @"9 minutes ago", @"As always. :)", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]],

                                                           [NSDictionary dictionaryWithObjects:
                                                            [NSArray arrayWithObjects:@"Luis", @"2 minutes ago", @":D", @"http://g-ecx.images-amazon.com/images/G/01/electronics/detail-page/Klipsch-Image-S4-II-Black-Lifestyle.jpg", nil]
                                                                                       forKeys:
                                                            [NSArray arrayWithObjects:@"name", @"dateline", @"body", @"image_url", nil]]



                                                           , nil] , nil]

                                                 forKeys:[NSArray arrayWithObjects:@"photo", @"credit",@"credit_label", @"name", @"comments", nil]
                      ]];

我的问题是,如何返回每个部分的 cmets 数量。因为 cmets 数组的内容数量或计数不同。

【问题讨论】:

NSLog(@"Comments: %d", [cmetsArray count]);返回 5,3,2 和 3 我尝试在 numberOfRowsInSection 中返回 2 并且成功了。但我没有得到预期的结果。它为所有部分返回 2 行。我也尝试返回 1 或 3,但都犯了与上面发布的相同的错误。 【参考方案1】:

修复了问题:

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
NSArray *commentsArray=[[dummyData objectAtIndex:section] valueForKeyPath:@"comments"];
NSLog(@"Comments: %d", [commentsArray count]);

if (section == didSection) 
    return [[[dummyData objectAtIndex:section] valueForKeyPath:@"comments"] count];

return 0;

【讨论】:

这与您在问题中发布的代码完全相同

以上是关于NSInternalInconsistencyException 原因:'无效更新:第 0 节中的行数无效'的主要内容,如果未能解决你的问题,请参考以下文章

无法在包中加载 Nib - 错误的 nibname 错误