Xcode 9中的自定义表格查看单元格

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

(我在每个UITableViewCell都有一两句话)

我以前用过这个:

 // 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 } 

…但是能够对其进行评论,因为根据iOS 11更新你的应用程序说默认会自动resize:

在此处输入图像描述

我已经尝试过将部署目标更改为iOS 11,在Storyboard中玩游戏(但我使用的是Table View Cell样式Basic,因此没有太多AutoLayout要做),我无法弄清楚是什么正在进行。

我将UILabel标题设置为0行,并且具有换行字换行,但仍然没有得到任何接近于基于Xcode 9中的文本内容扩展单元格。任何想法?

谢谢!

编辑:

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

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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

 class MyTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableView.estimatedRowHeight = UITableViewAutomaticDimension tableView.rowHeight = UITableViewAutomaticDimension } 

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

更新

Xcode 9 beta 3中的新function:

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

我有相同的破桌视图问题。 修复只需点击一下。

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

在此处输入图像描述

请注意,我还在UITableViewController子类的viewDidLoad中指定了以下内容(layoutSubviews解决了第一次加载tableViewController时未能正确定位与非半透明navBar相关的问题)。

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

此外

 tableView.estimatedRowHeight = UITableViewAutomaticDimension tableView.rowHeight = UITableViewAutomaticDimension 

你应该为tabeleViewCell的contentView设置一个高度约束。

 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 } } 

我得到了同样的问题,我在许多文档中读到了它,满意的答案是这样的,你必须检查两个选项才能获得正确的高度,因为初始UI设置需要估计高度,如滚动视图栏和其他此类东西。

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

看故事板的这张图片

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

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

像这样的东西。 谢谢!

 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() } } 

对我来说,安全区已经过检查。 取消选中“安全区”为我做了工作。

“安全区”