【发布时间】:2016-07-05 03:38:21
【问题描述】:
提前致谢。请帮我找出错误。
在我的应用程序中,我有可扩展的表格视图。当我尝试在部分中显示所有行时,应用程序崩溃了。
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;
}
【问题讨论】:
-
您不应该自己调用数据源或委托方法。您正在改变数组并再次调用方法。使用您自己的数组计数,而不是调用
self tableView:tableView numberOfRowsInSection:section] -
我尝试了你的建议,但仍然遇到同样的错误。我认为错误在 [tableView insertRowsAtIndexPaths:tmpArray withRowAnimation:UITableViewRowAnimationTop];我无法识别它。
标签: ios objective-c xcode uitableview