Xcode 9 中的自调整表格视图单元格

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

Self-Sizing Table View Cell in Xcode 9

iosswiftxcodeuitableviewautolayout

提问by SRMR

I have a UITableViewControllerwhere the cell's self sized correctly using Xcode 8 and Swift 3. Now that I'm using Xcode 9 and Swift 4, they aren't expandingand are just using the default height of 44.

我有一个UITableViewController使用 Xcode 8 和 Swift 3 正确调整单元格大小的地方。现在我使用的是Xcode 9 和 Swift 4,它们没有扩展,只是使用默认高度 44。

(I have about a sentence or two in each UITableViewCell)

(我每篇大约有一两句话UITableViewCell

I was using this before:

我之前用过这个:

// MARK: - Table view delegate

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

... but was able to comment it out because per Updating Your App for iOS 11said that the default would be self-sizing now:

...但能够将其注释掉,因为每个更新您的 iOS 11 应用程序都说默认值现在是自我调整大小:

enter image description here

在此处输入图片说明

I've tried playing around with changing the deployment target to iOS 11, playing around in Storyboard (but I'm using a Table View Cell style Basic so there is not much AutoLayout to be done), and I can't figure out what is going on.

我已经尝试将部署目标更改为 iOS 11,在 Storyboard 中进行操作(但我使用的是 Table View Cell 样式 Basic,因此没有太多 AutoLayout 要做),但我不知道是什么正在进行。

I have the UILabeltitle set to 0 Lines, and have Line Break Word Wrap, but still not getting anywhere close to getting the cell to expand based on the text content inside of it in Xcode 9. Any ideas?

我将UILabel标题设置为 0 行,并设置了换行符自动换行,但仍然无法根据 Xcode 9 中的文本内容扩展单元格。有任何想法吗?

Thanks!

谢谢!

Edit:

编辑:

Here's the options (that I don't have) for pinning since it is a Basic cell:

这是用于固定的选项(我没有),因为它是一个基本单元格:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Micha C.

I had the same problem and solved it with to lines of code:

我遇到了同样的问题并用代码行解决了它:

class MyTableViewController: UITableViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.estimatedRowHeight = UITableViewAutomaticDimension
    tableView.rowHeight = UITableViewAutomaticDimension
  }

Maybe it is a bug in Xcode.

也许这是Xcode中的一个错误。

Update

更新

New in Xcode 9 beta 3:

Xcode 9 beta 3 中的新功能:

Interface Builder now supports setting the estimatedRowHeight of UITableView. This allows self-sizing table cells by setting the estimated height to a value other than zero, and is on by default. (17995201)

Interface Builder 现在支持设置 UITableView 的估计行高度。这允许通过将估计高度设置为零以外的值来调整表格单元格的大小,并且默认情况下处于启用状态。(17995201)

回答by Mike Critchley

I had the same broken table view issue. Fix was just one click.

我有同样的损坏的表格视图问题。只需单击一下即可修复。

Go to your xib or storyboard scenes with table views, go to the size inspector, and you'll see the table view heights (even on dynamic table views) as 44, and sections will be 22. Just click "automatic" and boom, it will present as expected.

转到带有表格视图的 xib 或故事板场景,转到尺寸检查器,您将看到表格视图高度(即使在动态表格视图上)为 44,而部分将为 22。只需单击“自动”和繁荣,它将按预期呈现。

enter image description here

在此处输入图片说明

Note that I also specify the following in viewDidLoad of the UITableViewController subclass (layoutSubviews solves issues with the first load of a tableViewController not positioning correctly in relation to a non-translucent navBar).

请注意,我还在 UITableViewController 子类的 viewDidLoad 中指定了以下内容(layoutSubviews 解决了 tableViewController 的第一次加载没有正确定位到非半透明导航栏的问题)。

   self.tableView.estimatedRowHeight = 180;
   self.tableView.rowHeight = UITableViewAutomaticDimension;
   self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  [self.tableView layoutSubviews];

回答by Najam

I got the same issue and I read about it in many documentation, satisfying answer was something like this, You have to check both options in order to get proper height, because estimated height is needed for initial UI setup like scrollview bars and other such stuff.

我遇到了同样的问题,我在许多文档中都读到了它,令人满意的答案是这样的,您必须检查两个选项才能获得正确的高度,因为初始 UI 设置(例如滚动条和其他此类内容)需要估计高度.

Providing a nonnegative estimate of the height of rows can improve the performance of loading the table view. If the table contains variable height rows, it might be expensive to calculate all their heights when the table loads. Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time. When you create a self-sizing table view cell, you need to set this property and use constraints to define the cell's size. The default value is 0, which means there is no estimate. (Apple Documentation)>

提供行高的非负估计可以提高加载表视图的性能。如果表格包含可变高度的行,则在加载表格时计算它们的所有高度可能会很昂贵。使用估计允许您将几何计算的一些成本从加载时间推迟到滚动时间。当你创建一个自定大小的表格视图单元格时,你需要设置这个属性并使用约束来定义单元格的大小。默认值为 0,表示没有估计。(苹果文档)>

see this image for storyboard

看到这张图片的故事板

Also note that there is a bug in xCode 9, when you try to apply Lazy loading in automatic height calculation, it will scroll unexpectedly, so I'll recommend you to use programmatic way in this regard.

另请注意,xCode 9 中存在一个错误,当您尝试在自动高度计算中应用延迟加载时,它会意外滚动,因此我建议您在这方面使用编程方式。

self.postTableView.estimatedRowHeight = 200;
self.postTableView.rowHeight = UITableViewAutomaticDimension;

something Like this. Thanks!

像这样的东西。谢谢!

回答by hiren

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tblview: UITableView!

    var selectindex = -1

    var arrnumber  = ["1","2","3","4","5"]

    var image = ["index.jpg","rose-blue-flower-rose-blooms-67636.jpeg","index.jpg","rose-blue-flower-rose-blooms-67636.jpeg","index.jpg"]


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return arrnumber.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tblview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)as! ExpandableTableViewCell


        cell.lblnumber.text = arrnumber[indexPath.row]

        cell.img.image = UIImage(named: image[indexPath.row] as! String)

        return cell


    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if (selectindex == indexPath.row)
        {
            return 250
        }
        else{
            return 60
        }

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if(selectindex == indexPath.row)
        {
            selectindex = -1
        }else{
            selectindex = indexPath.row
        }

        self.tblview.beginUpdates()
        self.tblview.endUpdates()

    }

}

回答by sRoy

For me, Safe Area was checked. Unchecking "Safe Area" did the work for me.

对我来说,安全区被检查了。取消选中“安全区域”对我有用。

"Safe Area"

“安全区”

回答by Lindemann

In addition to

此外

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension

you should set a height constraint for the contentViewof the tabeleViewCell.

您应该为contentViewtabeleViewCell 的设置高度约束。

class CustomTableViewCell: UITableViewCell {
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        let height: CGFloat = 200
        heightAnchor.constraint(equalToConstant: height).isActive = true
    }
}