提前致谢。请帮我找出错误。
在我的应用程序中,我有可扩展的tableview。当我尝试显示部分中的所有行时,应用程序崩溃。
for (int i=0; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
但如果循环类似于 tableView :( UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
,则可以正常工作for (int i=1; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(6)必须等于更新前该部分中包含的行数(1),加上或减去从该部分插入或删除的行数(插入6个,删除0个)以及加入或减去移入或移出该部分的行数( 0移入,0移出)。'
*** First throw call stack:
(
0 CoreFoundation 0x02d9c5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x02b1f8b6 objc_exception_throw + 44
2 CoreFoundation 0x02d9c448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x0270023e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x0196320d -[UITableView _endCellAnimationsWithContext:] + 13402
5 UIKit 0x01972c60 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 337
6 UIKit 0x01972c9f -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 55
7 UIKit 0x0dceada5 -[UITableViewAccessibility(Accessibility) insertRowsAtIndexPaths:withRowAnimation:] + 65
8 BPSSalesDocs 0x00222d10 -[MiddlePageViewController_iPhone tableView:didSelectRowAtIndexPath:] + 1152
9 UIKit 0x01973ec1 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1513
10 UIKit 0x01974034 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 279
11 UIKit 0x01978018 __38-[UITableView touchesEnded:withEvent:]_block_invoke + 43
12 UIKit 0x018aed33 ___afterCACommitHandler_block_invoke + 15
13 UIKit 0x018aecde _applyBlockToCFArrayCopiedToStack + 403
14 UIKit 0x018aeb0a _afterCACommitHandler + 532
15 CoreFoundation 0x02d6453e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
16 CoreFoundation 0x02d6448f __CFRunLoopDoObservers + 399
17 CoreFoundation 0x02d423b4 __CFRunLoopRun + 1076
18 CoreFoundation 0x02d41b33 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x02d4194b CFRunLoopRunInMode + 123
20 GraphicsServices 0x04b449d7 GSEventRunModal + 192
21 GraphicsServices 0x04b447fe GSEventRun + 104
22 UIKit 0x0189294b UIApplicationMain + 1225
23 BPSSalesDocs 0x001c8a3d main + 141
24 libdyld.dylib 0x03822725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
以下是UITable View didSelectRowAtIndexPath
的代码- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_ParentCollection = [[Content alloc] initWithDictionary:[_collections objectAtIndex:indexPath.section]];
[self performSelectorOnMainThread:@selector(loadChildCollection:) withObject:[_ParentCollection collectionId] waitUntilDone:YES];
if ([_childCollections count] > 0) {
if ([self tableView:tableView canCollapseSection:indexPath.section]) {
if (!indexPath.row) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSInteger section = indexPath.section;
BOOL currentlyExpanded = [_expandedSections containsIndex:section];
NSInteger rows;
NSMutableArray *tmpArray = [NSMutableArray array];
if (currentlyExpanded)
{
rows = [self tableView:tableView numberOfRowsInSection:section];
[_expandedSections removeIndex:section];
}
else
{
[_expandedSections addIndex:section];
rows = [self tableView:tableView numberOfRowsInSection:section];
}
for (int i=0; i<rows; i++)
{
NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
inSection:section];
[tmpArray addObject:tmpIndexPath];
}
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
}
else {
self.childCollectionContent = [[Content alloc] initWithDictionary:[self.childCollections objectAtIndex:indexPath.row]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:[DeviceHelper getStoryboardName] bundle:nil];
ContentViewController *contentViewController = (ContentViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"ContentViewController"];
[contentViewController setNavigationalContents:_childCollections];
[contentViewController setCollectionId:[NSString stringWithFormat:@"%@", [self.childCollectionContent collectionId]]];
[contentViewController setContentId:[self.childCollectionContent contentId]];
[contentViewController setTitle:[self.childCollectionContent title]];
[contentViewController setContentType:[self.childCollectionContent type]];
// [contentViewController setContentUpdateDelegate:_contentUpdateDelegate];
// [contentViewController setContentNavigationDelegate:_contentNavigationDelegate];
if ([[[AppService sharedService] mainViewController] isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)[[AppService sharedService] mainViewController];
navigationController.navigationBarHidden = NO;
[navigationController pushViewController:contentViewController animated:YES];
NSDictionary *attributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
[[navigationController navigationBar] setTitleTextAttributes:attributes];
} else {
[[[AppService sharedService] mainViewController] presentViewController:contentViewController animated:YES completion:nil];
}
AnalyticsClicked* analyticsClicked = [[AnalyticsClicked alloc] init];
analyticsClicked.collectionId = [self.childCollectionContent collectionId];
analyticsClicked.contentId = [self.childCollectionContent contentId];
analyticsClicked.contentNumber = [self.childCollectionContent contentNumber];
[[[AppService sharedService] analytics] platformAnalyticsContentClickPush:analyticsClicked];
}
}
}
}
和numberOfRowsInSections代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// return [_collections count];
if ([self tableView:tableView canCollapseSection:section]) {
if ([_expandedSections containsIndex:section]) {
return [_childCollections count];
}
}
return 1;
}
答案 0 :(得分:1)
我终于能够克服那个错误。 在我的 didSelectRowAtIndexPath 中,我更改了以下代码
if (currentlyExpanded)
{
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
else
{
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
}
与
if (currentlyExpanded)
{
[tableView beginUpdates];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];
[tableView deleteRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
}
else
{
[tableView beginUpdates];
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];
[tableView insertRowsAtIndexPaths:tmpArray
withRowAnimation:UITableViewRowAnimationTop];
[tableView endUpdates];
}
我认为问题是由于更新前和更新后的行数不匹配,只需通过调用即可解决
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]
重置行数。 如果有人想改进或纠正这个问题,请纠正我。