我的应用程序中的几个表格视图有问题。
当表格的内容发生变化(因此表格视图的内容变大)时,就会出现问题。
由于某种原因, UITableView 的 contentSize 保持不变,这意味着我无法滚动查看新内容。
当特定单元格变大(在 [tableView reloadData]; 之后)或添加新单元格(使用 NSFetchedResultsController 委托方法)时,会发生这种情况。
我需要为 tableView 做些什么来重新调整它的 contentSize 吗?
编辑以显示代码,但这只是样板 NSFetchedResultsController 委托代码......
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:(OJFPunchListCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];
}
我以前在iOS5中做过这种事情,从来没有遇到过问题。似乎这可能是 iOS6 的变化?
::编辑::
愚蠢的是,我在同一个应用程序中有许多其他 UITableViewControllers,它们都工作正常。我可以添加新单元格、添加动态大小的单元格、调整单元格大小等......并且表格在所有内容中都可以正确滚动。
这个问题现在发生在我最近添加的几个 tableview 上。
::编辑::
好的,只是做一些调试。当我第一次进入表视图时 contentSize.height 是 594。
当我添加一个新项目并返回到同一个 tableviewController 时,contentSize.height 是 749。
但是,我无法滚动查看 tableView 底部新添加的单元格。它只是不会在那里滚动。它在旧的 contentSize 高度反弹。