Fog*_*ter 17 iphone uitableview nsfetchedresultscontroller ios
在我的应用中遇到几个表视图的问题.
当表的内容发生更改(因此tableview的内容变大)时会发生问题.
出于某种原因,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];
}
Run Code Online (Sandbox Code Playgroud)
我之前在iOS5中做过这种事情,从来没有遇到过问题.似乎这可能是iOS6的变化?
::编辑::
关于这一点的愚蠢之处在于我在同一个应用程序中有许多其他UITableViewControllers,它们都运行良好.我可以添加新单元格,添加动态大小的单元格,调整单元格大小等等...并且所有内容都可以在所有内容中上下滚动.
这个问题现在发生在我最近添加的几个tableview上.
::编辑::
好的,只是做一些调试.当我第一次进入表格视图时,contentSize.height是594.
当我然后添加一个新项目并返回到同一个tableviewController时,contentSize.height是749.
但是,我无法滚动查看tableView底部新添加的单元格.它只是不会滚动到那里.它在旧的contentSize高度反弹.
Zol*_*tán 20
我一直遇到同样的问题,最终得到了这个解决方法.我设置contentSize之后[self.tableView endUpdates];
[self.tableView endUpdates];
NSLog(@"%f", self.tableView.contentSize.height);
self.tableView.contentSize = [self.tableView sizeThatFits:CGSizeMake(CGRectGetWidth(self.tableView.bounds), CGFLOAT_MAX)];
NSLog(@"%f", self.tableView.contentSize.height);
[self.tableView setContentInset:UIEdgeInsetsMake(50, 0, -50, 0)];
Run Code Online (Sandbox Code Playgroud)
小智 1
我使用带有两个子 VC 的自定义视图控制器时遇到了同样的问题,其中一个是 tableview + fetched results 控制器。
在从一个 vc 添加内容并返回到我的表格后,我将无法滚动到底部的新内容。
因此,在我的表视图 + 获取的结果控制器中,我根据获取的对象数量调整内容大小,无论它们来自哪里。
最坏的情况是我在展示 VC 时没有添加任何内容,但成本却是最小的。
//In my tableview + fetched results controller class
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSUInteger objCount = self.fetchedResultsController.fetchedObjects.count;
//My custom cell has a height of 50.0f, yours may not!
CGFloat tableHeight = objCount * 50.0f;
NSLog(@"%i objects, total height: %f",objCount,tableHeight);
[self.tableView setContentSize:CGSizeMake(self.tableView.contentSize.width, tableHeight)];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23958 次 |
| 最近记录: |