【问题标题】:Self-Sizing Table View Cell in Xcode 9Xcode 9 中的自定大小表格视图单元格
【发布时间】:2017-11-10 18:41:28
【问题描述】:

我有一个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
}

...但能够将其注释掉,因为每个 Updating Your App for iOS 11 说现在默认是自调整大小:

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

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

谢谢!

编辑:

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

【问题讨论】:

  • 如果你想让你的单元格扩展,内容必须用自动布局固定到单元格的顶部和底部,你这样做吗?
  • Xcode 9 和 iOS 11 SDK 仍处于测试阶段,因此会有错误。如果这是测试版软件中的错误,则应向 Apple 报告。
  • @EmilioPelaez 我在我原来的问题中添加了一些图片,关于我缺少选项,这是否澄清?谢谢!
  • @RoboticCat 对,如果我确定这是一个错误,我会这样做,但想知道是否有人知道我遗漏了一些特定的东西,或者你不知道?
  • 它是您需要添加约束的内容,而不是单元格本身,我认为这将是 iOS 10 XCode8 中自动DimensionCells 所需的相同约束,您是否调整了 content compression resistance prioritycontent hugging priority ?

标签: ios swift xcode uitableview autolayout


【解决方案1】:

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

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

    tableView.estimatedRowHeight = UITableViewAutomaticDimension
    tableView.rowHeight = UITableViewAutomaticDimension
  }

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

更新

Xcode 9 beta 3 中的新功能:

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

【讨论】:

  • 嗨!是的,这与我在 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension }func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } 的原始问题中提出的相同
  • 在 Xcode 9 beta 6 中将estimatedRowHeight 和 rowHeight 设置为 UITableViewAutomaticDimension 对我有用
  • 在 Xcode 版本 9.0.1 (9A1004) 中,IB 设置已经是自动的,但在第一次绘制时行被压缩,仅在下一次重绘时纠正高度。为了解决这个问题,我不得不插入你的代码行。
  • 我花了将近半天的时间才找到你的答案。第一次加载单元格永远不会动态调整高度,但后续加载甚至随着可访问性检查器正确呈现而改变。谢谢!
【解决方案2】:

我遇到了同样的损坏表视图问题。一键修复。

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

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

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

【讨论】:

  • 有用的答案^^
  • 此外,对于具有多行详细信息标签的 UITableViewCellStyleValue1 类型的非子类单元格,这似乎不适用于 iOS 11。
  • 它在iOS10中工作。尚未在设备上的 iOS11 中尝试过。我会在更新我的 iOS 后检查它(我从不急于进行那些早期更新,哈哈)。我使用的所有单元格都是动态加载的自定义 UITableViewCell 子类。我没有在 IB 中做任何表格视图 UI。
【解决方案3】:

除了

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

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,我在许多文档中都读到过,令人满意的答案是这样的,您必须检查这两个选项才能获得正确的高度,因为初始 UI 设置(如滚动视图栏和其他类似的东西。

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

    see this image for storyboard

    还要注意xCode 9有一个bug,当你尝试在自动高度计算中应用延迟加载时,它会意外滚动,所以我建议你在这方面使用编程方式。

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

    类似这样的东西。谢谢!

    【讨论】:

      【解决方案5】:
      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()
      
          }
      
      }
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 2017-06-09
          • 2017-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-08
          相关资源
          最近更新 更多