ios 如何在 UITableViewController 中设置静态单元格的自动高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29368299/
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
How to set automatic height of static cell in UITableViewController?
提问by bpiec
I am using IB and storyboard and I defined UITableViewController
. There I defined three staticcells. The first two should have a given height but the last one should automatically fill the remaining space (anchor to the bottom of the view). How can I achieve this? I can use autolayout for the controls insidethe cell but it is greyed out for the cell itself.
我正在使用 IB 和故事板,我定义了UITableViewController
. 在那里我定义了三个静态单元格。前两个应该有一个给定的高度,但最后一个应该自动填充剩余的空间(锚定到视图的底部)。我怎样才能做到这一点?我可以对单元格内的控件使用自动布局,但它对于单元格本身是灰色的。
The best solution would be to do this from IB but programmatic solutions are also welcomed.
最好的解决方案是从 IB 执行此操作,但也欢迎程序化解决方案。
EDIT:
编辑:
This is how it looks in my storyboard (colors are for visualizing separate controls, each control has required constraints):
这是它在我的故事板中的外观(颜色用于可视化单独的控件,每个控件都有所需的约束):
And after launching:
Note that the text view ends below the bottom of the screen.
启动后:
请注意,文本视图在屏幕底部下方结束。
I forgot to mention that I target iOS 7.0+.
我忘了提到我的目标是 iOS 7.0+。
采纳答案by Christos Chadjikyriacou
Ok try this
好的试试这个
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
if indexPath.row == 0 {
return firstRowHeight
}
else if indexPath.row == 1 {
return secondRowHeight
}
else {
return tableView.frame.size.height - firstRowHeight - secondRowHeight
}
}
The only thing that may be a problem is that this method is going to run before the view sets the tableView height
唯一可能有问题的是,此方法将在视图设置 tableView 高度之前运行
回答by Ryan Romanchuk
You can actually use UITableViewAutomaticDimension
with static cells. Design your static cells per usual and manually set your cells as dynamic heights by adding
您实际上可以UITableViewAutomaticDimension
与静态单元格一起使用。按照惯例设计您的静态单元格,并通过添加手动将您的单元格设置为动态高度
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 240 // Something reasonable to help ios render your cells
Now override the heightForRow
delegate
现在覆盖heightForRow
委托
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
The only other caveat I noticed with this technique is that you must use NSAutolayout
to create a constraint to the bottom container (the UITableViewCell
's container). So let's say you have a container inside the cell's contentView that will have a dynamic height, for this to render properly you need to create a bottom space constraint.
我注意到这种技术的唯一其他警告是您必须使用NSAutolayout
来创建对底部容器(UITableViewCell
的容器)的约束。因此,假设您在单元格的 contentView 中有一个具有动态高度的容器,为了正确呈现,您需要创建底部空间约束。
If you have some static cells that do have fixed heights I recommend returning that size by switching on row in tableView:heightForRowAtIndexPath:indexPath
如果您有一些具有固定高度的静态单元格,我建议通过打开行来返回该大小 tableView:heightForRowAtIndexPath:indexPath
回答by Mat0
Be sure to override heightForRowAtIndexPath! Otherwise it will not display correctly from my experiences
一定要覆盖 heightForRowAtIndexPath!否则根据我的经验它将无法正确显示
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
回答by Fogmeister
I covered this in a blog post using a static table view and setting the cell heights using auto layout. The cells are all defined in interface builder.
我在一篇使用静态表格视图并使用自动布局设置单元格高度的博客文章中介绍了这一点。这些单元格都在界面构建器中定义。
You have to enable the automatic cell heights in code but that's only a couple lines.
您必须在代码中启用自动单元格高度,但这只是几行。
I show everything including IB and code.
我展示了一切,包括 IB 和代码。
Take a look
看一看
http://www.oliverfoggin.com/using-a-static-uitableview-as-a-layout-device/
http://www.oliverfoggin.com/using-a-static-uitableview-as-a-layout-device/
回答by Riajur Rahman
If the whole tableview cell height is dynamic then you can try this
如果整个 tableview 单元格高度是动态的,那么你可以试试这个
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 240
or a tableview needs some fixed height cell & some dynamic height cell the you can checkout the answer Dynamic & static tableview cell height
或者 tableview 需要一些固定高度的单元格和一些动态高度的单元格,您可以查看答案 Dynamic & static tableview cell height
Hope it helps.
希望能帮助到你。
回答by Baron Ch'ng
Ryan Romanchuk's answer is great. I have a tip that may help beginner like myself.
Ryan Romanchuk 的回答很棒。我有一个提示可以帮助像我这样的初学者。
Since this is a static cell case, you might be inclined to use IBOutlet
for every dynamic field. Setting values in say UILabel
outside tableView
load cycle will not have the new height considered.
由于这是一个静态单元格案例,您可能倾向于将其IBOutlet
用于每个动态字段。UILabel
外部tableView
负载循环中的设置值不会考虑新的高度。
To allow the dynamic height to work, you can assign values in overridden cellForRowAtIndexPath
delegate so that autolayout can pick up the new height. Call tableView.reloadData()
when you want to update the fields.
为了允许动态高度工作,您可以在覆盖的cellForRowAtIndexPath
委托中分配值,以便自动布局可以选择新的高度。tableView.reloadData()
当您想要更新字段时调用。
回答by svrushal
Not sure from IB and using the Autolayout
来自 IB 和使用 Autolayout 不确定
But using programatically,
但是以编程方式使用,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Using this method u can check for the indexpath.row. And using this return the different height.
使用此方法您可以检查 indexpath.row。并使用此返回不同的高度。
回答by Tanuj
For static (non-data-driven) height, you can just dequeue the cell once and store the height:
对于静态(非数据驱动)高度,您只需将单元出列一次并存储高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSNumber *height;
if (!height) {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
height = @(cell.bounds.size.height);
}
return [height floatValue];
}