ios UITableView 内容大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13181607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView contentSize
提问by Fogmeister
Having a problem with a couple of table views in my app.
我的应用程序中的几个表视图有问题。
The problem happens when the contents of the table changes (and therefore the content of the tableview gets bigger).
当表格的内容发生变化时(因此表格视图的内容变大),就会出现问题。
For some reason the contentSize of the UITableView stays the same meaning I can't scroll to see the new content.
出于某种原因, UITableView 的 contentSize 保持不变,这意味着我无法滚动查看新内容。
This happens either when a particular cell gets bigger (after a [tableView reloadData];) or when I add new cells (using the NSFetchedResultsController delegate methods).
当特定单元格变大时(在 [tableView reloadData]; 之后)或当我添加新单元格(使用 NSFetchedResultsController 委托方法)时,就会发生这种情况。
Is there something I need to do for the tableView to readjust its contentSize?
我需要为 tableView 做些什么来重新调整其 contentSize 吗?
Edit to show code but this is just boilerplate NSFetchedResultsController delegate code...
编辑以显示代码,但这只是样板 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];
}
I've done this kind of thing before in iOS5 and never had a problem with it. Seems this is potentially an iOS6 change?
我以前在iOS5中做过这种事情,从来没有遇到过问题。似乎这可能是 iOS6 的更改?
::EDIT::
::编辑::
The stupid thing about this is that I have a number of other UITableViewControllers in the same app and they are all working fine. I can add new cells, add dynamically sized cells, resize cells, etc... and the tables all scroll properly all the way up and down all the content.
关于这个的愚蠢之处在于我在同一个应用程序中有许多其他 UITableViewControllers,它们都运行良好。我可以添加新单元格、添加动态大小的单元格、调整单元格大小等......并且表格都可以正确地上下滚动所有内容。
This problem is now happening on a couple of tableview that I've added recently.
这个问题现在发生在我最近添加的几个 tableview 上。
::EDIT::
::编辑::
OK, just doing some debugging. When I first go into the Table View the contentSize.height is 594.
好的,只是做一些调试。当我第一次进入表格视图时,contentSize.height 是 594。
When I then add a new item and go back into the same tableviewController the contentSize.height is 749.
当我添加一个新项目并返回到同一个 tableviewController 时,contentSize.height 是 749。
However, I can't scroll to see the newly added cell at the bottom of the tableView. It just wont' scroll there. It bounces at the old contentSize height.
但是,我无法滚动查看 tableView 底部新添加的单元格。它只是不会在那里滚动。它以旧的 contentSize 高度反弹。
回答by Zoltán
I've been running into the same problem and ended up with this workaround. I'm setting the contentSize
right after [self.tableView endUpdates];
我遇到了同样的问题,最终得到了这个解决方法。我正在设置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)];
回答by shazgoose
I had the same problem using a custom view controller with two child VC's, one being a tableview + fetched results controller.
我在使用带有两个子 VC 的自定义视图控制器时遇到了同样的问题,一个是 tableview + fetched results 控制器。
I would get to the point after adding content from one vc and going back to my table that I wouldn't be able to scroll to new content at bottom.
在从一个 vc 添加内容并返回到我的表格后,我将切入这一点,我将无法滚动到底部的新内容。
So in my tableview + fetched results controller I adjust the content size based off of the number of fetched objects I have, regardless of where they came from.
所以在我的 tableview + fetched results 控制器中,我根据我拥有的获取对象的数量调整内容大小,无论它们来自哪里。
Worst case scenario is that I didn't add any content when showing the VC, but the cost is minimal.
最坏的情况是我在展示 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)];
}
回答by Djordje Dozet
I have dynamic tableView with 'UITableViewAutomaticDimension'. Something happens and after reload and it's content size is equal to its frame size. After i manualy put content size to be as table bounds size, everything works. I hope this helps.
我有带有“UITableViewAutomaticDimension”的动态 tableView。发生了一些事情,重新加载后,它的内容大小等于其帧大小。在我手动将内容大小设置为表格边界大小后,一切正常。我希望这有帮助。
table.beginUpdates()
table.contentSize = CGSize(width: table.bounds.width, height: table.bounds.height)
table.endUpdates()
And this is after I done table.reloadData()
这是在我完成 table.reloadData() 之后
回答by Ted Guo
Try this:
尝试这个:
I've met with simular issue.In my situation,I drag a tableview to a custom view controller,which is presented by a push segue.If the amount of data showed in tableview exceeds some number,then I can't touch the cell on bottom of the tableview.
我遇到了类似的问题。在我的情况下,我将一个 tableview 拖动到一个自定义视图控制器,由 push segue 呈现。如果 tableview 中显示的数据量超过某个数字,那么我无法触摸单元格在 tableview 的底部。
Many ways have been tried:set the frame/content size of the table view,and none works for me.Finally,I find the root cause and solve it in a simple way(although not gracefully).
尝试了很多方法:设置表格视图的框架/内容大小,但没有一个对我有用。最后,我找到了根本原因并以简单的方式解决了它(虽然不是很优雅)。
First,the root cause:the table view created by IB has a width larger then the its parent view controller.Thus,any cell out of view's bound will not be touched.
首先,根本原因:IB创建的表格视图的宽度大于其父视图控制器的宽度。因此,任何超出视图边界的单元格都不会被触及。
So,the solution is simple:Go to StoryBoard,adjust the width of table view,making it smaller than the width of parent view.It seems that if one table view is created by StoryBoard,you can't change its frame by code.That's what I've found up to now. I guess it's a bug of StoryBoard.
所以,解决办法很简单:去StoryBoard,调整table view的宽度,使其小于父view的宽度。看来如果StoryBoard创建了一个table view,就不能通过代码改变它的frame了。这就是我目前发现的。我猜这是 StoryBoard 的一个错误。
Hope it be useful for you.
希望对你有用。