gpt4 book ai didi

ios - 未插入 UITable 的行

转载 作者:行者123 更新时间:2023-11-29 13:38:10 27 4
gpt4 key购买 nike

我有一个带有展开/折叠部分的表格 View 的应用程序,遵循 Apple 的 Table View Animations & Gestures sample app 中的示例。 .当一个项目被添加到一个关闭的部分时我遇到了问题:之后,该部分不再打开,当我尝试打开然后关闭它时出现异常。

我已经将其追溯到打开/关闭方法中的一些奇怪行为:

-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)section {

if (![[sectionHeaderArray objectAtIndex:section] isOpen]) {

[[sectionHeaderArray objectAtIndex:section] setIsOpen:YES];

NSLog(@"self.tableView: %@", self.tableView);
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger countOfRowsToInsert = [sectionInfo numberOfObjects];

NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}

// Apply the updates.
[self.tableView beginUpdates];
NSLog(@"Count of rows to insert: %d", [indexPathsToInsert count]);
NSLog(@"Rows before insert: %d", [self.tableView numberOfRowsInSection:section]);
[self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationTop];
NSLog(@"Rows after insert: %d", [self.tableView numberOfRowsInSection:section]);
[self.tableView endUpdates];
}

}


-(void)sectionHeaderView:(SectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)section {

if ([[sectionHeaderArray objectAtIndex:section] isOpen]) {

[[sectionHeaderArray objectAtIndex:section] setIsOpen:NO];

NSInteger countOfRowsToDelete = [self.tableView numberOfRowsInSection:section];

if (countOfRowsToDelete > 0) {
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
[self.tableView beginUpdates];
NSLog(@"Count of rows to delete: %d", [indexPathsToDelete count]);
NSLog(@"Rows before delete: %d", [self.tableView numberOfRowsInSection:section]);
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];
NSLog(@"Rows after delete: %d", [self.tableView numberOfRowsInSection:section]);

}

[self.tableView endUpdates];
}
}

日志消息显示,在打开(插入行)时,插入了 >0 行,但该部分的行数仍为 0:

2012-03-31 13:36:17.454 QuickList7[5523:fb03] Count of rows to insert: 3
2012-03-31 13:36:17.454 QuickList7[5523:fb03] Rows before insert: 0
2012-03-31 13:36:17.454 QuickList7[5523:fb03] Rows after insert: 0

这在表和数据源之间设置了不一致的状态,然后当我尝试“折叠”该部分时,出现以下异常:

2012-03-31 13:48:35.783 QuickList7[5523:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid table view update.  The application has requested an update to the table view that is inconsistent with the state provided by the data source.'

我怎样才能插入 3 行,最后仍然是 0 行?

谢谢,

萨沙

最佳答案

我找到问题了!它实际上在 fetchedResultsController 的更改处理程序中。它正在响应对关闭部分的更改,这使表处于错误状态,并且与数据源不同步。所以我为每个更新添加了一个检查,以仅在包含部分打开时插入/删除/更新行。

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tv = self.tView;
switch(type) {
case NSFetchedResultsChangeInsert:
if ([[sectionHeaderArray objectAtIndex:newIndexPath.section] isOpen]) {
[tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;

case NSFetchedResultsChangeDelete:
if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) {
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;

case NSFetchedResultsChangeUpdate:
if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) {
[self configureCell:[tv cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
}
break;

case NSFetchedResultsChangeMove:
if ([[sectionHeaderArray objectAtIndex:indexPath.section] isOpen]) {
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
if ([[sectionHeaderArray objectAtIndex:newIndexPath.section] isOpen]) {
[tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
}
break;
}

}

关于ios - 未插入 UITable 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9958234/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com