【发布时间】:2012-10-22 07:23:24
【问题描述】:
我的应用中的几个表格视图出现问题。
当表格的内容发生变化(因此表格视图的内容变大)时,就会出现问题。
由于某种原因,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 高度反弹。
【问题讨论】:
-
如果您为
heightForRow:atIndexPath:中的单元格提供正确的高度应该没问题。当单元格应该变大时,您是否更新了高度? -
我在添加单元格时不需要修改内容大小。你使用什么方法?插入行?你能显示一些代码吗?
-
是的,随着实际单元格变大,我正在正确设置高度。但是表格视图的底部不会滚动到单元格变大的数量。即如果单元格变大 50 点,那么我无法滚动查看单元格的底部 50 点。另外,我要添加新单元格的表格,所有单元格的大小都相同,只是更多。 (就像主细节模型一样,主表视图有一个 + 按钮可以推送到控制器以创建一个新项目,然后显示在主视图中)新单元格在那里,但滚动视图弹跳,所以我看不到它。
-
如果 contentSize 值是正确的并且表格框架是正确的,那么也许 contentInset 是原因?
-
检查
tableView:heightForRowAtIndexPath:,在我的例子中这个方法返回负值。
标签: iphone ios uitableview nsfetchedresultscontroller