简体   繁体   English

表格视图单元格的大小取决于情节提要中预览设备的大小

[英]table view cell size depends on the preview device size in storyboard

I'm running my app on my iPhone 6 plus which has a screen width of 414 points, if I set the preview device to iPhone 7 which has a screen width of 375 points, when the table is first loaded, I printed out the size of the table in viewdidload and it had a width of 414, but the table cells all had a width of 375, after I scrolled up and back, the width changed to 414. Tested in multiple UITableViewController, all gave the same result. 我在屏幕宽度为414点的iPhone 6 plus上运行我的应用程序,如果我将预览设备设置为屏幕宽度为375点的iPhone 7,则在首次加载表格时,我会打印出尺寸在viewdidload中表的宽度为414,但表单元格的宽度均为375,在我上下滚动后,宽度更改为414。在多个UITableViewController中进行测试,所有结果均相同。

My questions are: 我的问题是:

Why is the size of the table view cell dependent on the preview device size? 为什么表视图单元格的大小取决于预览设备的大小?

how do I make the cell have the correct size when it's first loaded? 我如何使电池在首次加载时具有正确的尺寸?

tableView.layoutIfNeeded(), tableView.layoutSubViews() don't work. tableView.layoutIfNeeded(),tableView.layoutSubViews()不起作用。

The tableview has the correct size 414 (my device size) but somehow the cell size is the size of the simulated metrics, after a couple months of researching, this is the answer I came up with: tableview具有正确的大小414(我的设备大小),但是以某种方式,像元大小是模拟指标的大小,经过几个月的研究,这是我想出的答案:

In my cell class, override awakeFromNib() , set frame.size.width to UIScreen.main.bounds.width , then call layoutIfNeed() 在我的单元格类中,重写awakeFromNib() ,将frame.size.width设置为UIScreen.main.bounds.width ,然后调用layoutIfNeed()

Every time a view is loaded from Storyboard, it will initially have EXACTLY the size that was specified in the storyboard, then when it's added to the view hierarchy, its frame will be adjusted (either by Autolayout or your custom code). 每次从情节提要板加载视图时,其最初将具有情节提要板中指定的大小,然后将其添加到视图层次结构时,将调整其框架(通过自动布局或您的自定义代码)。

So in your case, when a cell is first loaded from storyboard, it will have 414 point in width (assumingly your tableView has the same width with the view controller), but when it is put in the actually view hierarchy, it will be resized accordingly (375pt, which is equal to your tableview's width). 因此,在您的情况下,当第一次从情节提要中加载单元格时,它将具有414点的宽度(假设您的tableView与视图控制器具有相同的宽度),但是当将其放置在实际的视图层次结构中时,它将被调整大小相应地(375pt,等于您的表格视图的宽度)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM