Xcode 9中的自定义表格查看单元格 [英] Self-Sizing Table View Cell in Xcode 9

查看:259
本文介绍了Xcode 9中的自定义表格查看单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



(每个 UITableViewCell )



我之前使用过这个:

  // MARK: - 表视图委托

覆盖func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath) - > CGFloat {
返回UITableViewAutomaticDimension
}

覆盖func tableView(_ tableView:UITableView,estimatedHeightForRowAt indexPath:IndexPath) - > CGFloat {
返回UITableViewAutomaticDimension
}

...但是能够发表评论因为



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



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



谢谢!



编辑:



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







解决方案

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

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

tableView.estimatedRowHeight = UITableViewAutomaticDimension
选项卡leView.rowHeight = UITableViewAutomaticDimension
}

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



更新



Xcode 9 beta新功能3:



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


I have a UITableViewController where 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 expanding and are just using the default height of 44.

(I have about a sentence or two in each 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 11 said that the default would be self-sizing now:

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.

I have the UILabel title 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?

Thanks!

Edit:

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

解决方案

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.

Update

New in 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)

这篇关于Xcode 9中的自定义表格查看单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆