xcode 动态设置tableview单元格行高?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37890804/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:55:50  来源:igfitidea点击:

Set tableview cell row height dynamically?

xcodeswiftuitableviewuiimageviewtableviewcell

提问by johnjay22113

I have a tableview with a label and an image. in some cells, there is no image as i used imageView.removeFromSuperview()to remove it from the cell. When there is an image in the cell, the row height is 445 and "Custom" is checked off.

我有一个带有标签和图像的 tableview。在某些单元格中,没有图像,因为我曾经imageView.removeFromSuperview()将其从单元格中删除。当单元格中有图像时,行高为 445,并选中“自定义”。

how can i set the row height dynamically according to how long the label is instead of how long/big the imageview is after i remove the imageview?

如何根据标签的长度而不是删除图像视图后图像视图的长度/大小动态设置行高?

回答by Rob

If you want dynamic row height, you can define your constraints (making sure they're unambiguous), set the label's numberOfLinesto zero, and then in viewDidLoad, tell it that rows should automatically adjust their height:

如果你想要动态行高,你可以定义你的约束(确保它们是明确的),将标签设置numberOfLines为零,然后在 中viewDidLoad,告诉它行应该自动调整它们的高度:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44

If you want to hide/show a UIImageViewas well, I must confess that I'm not crazy about the removeFromSuperviewapproach (because when the cell is reused, you have to re-add the image view, and possibly rebuilding its constraints, too) there are a few options:

如果你也想隐藏/显示 a UIImageView,我必须承认我对这种removeFromSuperview方法并不感冒(因为当单元格被重用时,你必须重新添加图像视图,并可能重建它的约束)那里有几个选项:

  1. You could have a different cell prototype for the cell with the image view and another without the image view. Then cellForRowAtIndexPathjust needs to instantiate the right cell.

  2. You could go ahead and define a full set of constraints that are unambiguous for both the presence of the image and without the image. And then, you can activatethe constraint and set the image view to hiddenas appropriate:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
    
        let image = ...  // let's say it was an optional, set if needed, left `nil` if not
    
        cell.customImageView?.image = image
    
        if image == nil {
            cell.customImageView.hidden = true
            cell.imageBottomConstraint.active = false
    
            cell.customLabel.text = ...
        } else {
            cell.customImageView.hidden = false
            cell.imageBottomConstraint.active = true
        }
    
        return cell
    }
    

    The trick when having competing sets of constraints that dictate the height of the cell is to just make sure that they have different priorities and/or they use inequality so if both sets are in effect, you don't have an unsatisfiable conflict (e.g. the image view might have higher priority).

  3. You can, if you want, go "old school" and programmatically determine the size of the label field, and then implement heightForRowAtIndexPath, but auto layout makes this process unnecessary.

  1. 您可以为带有图像视图的单元格使用不同的单元格原型,而另一个没有图像视图的单元格原型。然后cellForRowAtIndexPath只需要实例化正确的单元格。

  2. 您可以继续定义一整套约束,这些约束对于图像的存在和不存在图像都是明确的。然后,您可以activate约束并将图像视图设置hidden为适当的:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell
    
        let image = ...  // let's say it was an optional, set if needed, left `nil` if not
    
        cell.customImageView?.image = image
    
        if image == nil {
            cell.customImageView.hidden = true
            cell.imageBottomConstraint.active = false
    
            cell.customLabel.text = ...
        } else {
            cell.customImageView.hidden = false
            cell.imageBottomConstraint.active = true
        }
    
        return cell
    }
    

    当具有决定单元格高度的竞争约束集时,诀窍是确保它们具有不同的优先级和/或它们使用不等式,因此如果两个集都有效,则不会有不可满足的冲突(例如图像视图可能具有更高的优先级)。

  3. 如果需要,您可以“老派”并以编程方式确定标签字段的大小,然后实施heightForRowAtIndexPath,但自动布局使此过程变得不必要。

回答by Duly Kinsky

You need to override this method:

您需要覆盖此方法:

Override func tableview(tableview:UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {}

Return the desired height for the indexPath you want in this method

在此方法中返回所需的 indexPath 的所需高度

回答by rockiesGrizzly

If you're using UILabel, it has a frame with a height property.

如果您使用 UILabel,它有一个具有高度属性的框架。

And heightForRowAtIndexPath, should cover what you're wanting for the appropriate method to use: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:

和 heightForRowAtIndexPath,应该涵盖您想要使用的适当方法的内容:https: //developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/ tableView:heightForRowAtIndexPath:

not sure how your image is currently set up, but here's a rough example:

不确定您的图像当前是如何设置的,但这是一个粗略的示例:

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if image != nil {
            return 445.0
        else {
            label.frame.height (or whichever value you'd prefer)
        }
}