我有一个 iOS 8 应用程序,其中有一个搜索结果页面,每个单元格都会根据某个标签的高度自动调整大小.但是,在视图首次加载后,显示的单元格不会自动调整大小.向下滚动后,随后的单元格大小正确.我正在我的故事板中连接自动布局约束.我在 viewDidLoad 中有以下代码.
I have an iOS 8 application where I have a search results page that each cell is autosized based on the height of a certain label. However, after the view first loads the cells that are displayed don't autosize. After you scroll down the cells that follow are sized correctly. I am wiring up the auto layout constraints in my storyboard. I have the following code in viewDidLoad.
- (void)viewDidLoad {
[super viewDidLoad];
//configure tableView so that we use autolayout enabled row heights
self.tableView.estimatedRowHeight = 68;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
有其他人经历过吗?
是的,我在故事板中制作视图和约束时也遇到过同样的问题(但没有添加代码的视图).我已经通过在自定义单元类中添加此代码来解决此问题,
Yes, I've seen this same problem when making the views and constraints in the storyboard (but not with code added views). Iv'e fixed this by adding this code in the custom cell class,
-(void)didMoveToSuperview {
[self layoutIfNeeded];
}
这可能会去其他地方,但是这个方法似乎只调用了一次,所以我认为这是一个很好的地方.
This could probably go other places, but this method seems to be called only once, so I thought it was a good place to do it.
这篇关于iOS 8 自动高度单元格在第一次加载时高度不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!