【问题标题】:How to create static cells with dynamic cell heights with Swift如何使用 Swift 创建具有动态单元格高度的静态单元格
【发布时间】:2016-12-11 06:08:58
【问题描述】:

我查看了几个教程,这些教程展示了如何设置动态单元格高度,但所有这些教程都仅在您通过设置适当的约束并使用 UITableViewAutomaticDimension 来使用动态单元格时才显示。但是,我想对静态单元格执行此操作。

我的应用程序中有一个表格视图控制器,其中包含几个单元格,这些单元格显示一个带有披露指示器的类别,其中有一个标题和一个描述,描述文本的大小会有所不同。因此,我希望单元格的高度根据描述文本的大小是动态的。

我使用静态单元格而不是动态单元格的原因是因为在类别单元格上方我有一些单元格具有一些我只能通过使用静态单元格和情节提要获得的设计。

我正在使用iOS9Xcode 7.3Swift 2.2

更新

表格视图控制器代码

import UIKit

class CategoriesTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 140
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }

}

更新 2

根据下面接受的答案,我将以下两种方法添加到我的表格视图控制器中,并删除了我在 viewDidLoad() 中的 2 行。这是为我做的!请记住,我必须确保每个单元格中的所有标签都使用顶部、底部、前导和尾随约束。此外,每个标签的行数必须设置为 0。

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

【问题讨论】:

    标签: ios swift xcode7.3


    【解决方案1】:

    @Forge 回答 Swift 5 变体,

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

    【讨论】:

      【解决方案2】:

      UILabel:

      通过编程设置numberOfLines = 0 或在UILabel 的属性检查器中将行设置为零。

      UITextView:

      让我们选择您的文本视图并取消选中启用的滚动,如下图所示。

      将以下代码添加到您的视图控制器:

      斯威夫特 5:

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return UITableView.automaticDimension
      }
      
      func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
          return UITableView.automaticDimension
      }
      

      Swift 2.2:

       func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
         return UITableViewAutomaticDimension
       }
      
       func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
         return UITableViewAutomaticDimension
       }
      

      【讨论】:

      • 我没有使用文本视图。我正在为我的文本使用常规标签。
      • 但是,添加这两种方法确实解决了我的问题。现在正在根据标签文本的大小调整单元格的大小。谢谢!
      • 哦,好的。欢迎朋友 :) 让编译器开心!
      • 在故事板中,当我在静态表中检查“自动”行高时,故事板想要将高度强制为 44。如果我不将高度设置为 44,那么一切都会错位。应用运行时看起来是正确的,但是当高度不是 44 时,在界面生成器中布局非常困难。有其他人遇到过这种情况吗?
      【解决方案3】:

      @Forge,请添加到您的主题
      Swift 4 变体:

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

      【讨论】:

        【解决方案4】:

        对于 Swift 3

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

        【讨论】:

          【解决方案5】:

          您所要做的就是在cell 中正确设置AutoLayout,因此高度会根据描述文本的长度而增加。 然后你需要在你的viewDidLoad中设置以下内容

          tableview.rowHeight = UITableViewAutomaticDimension
          tableview.estimatedRowHeight = 44
          

          请注意:您需要将UILabelnumberOfLines设置为0

          【讨论】:

            猜你喜欢
            • 2020-12-06
            • 1970-01-01
            • 2020-02-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-26
            相关资源
            最近更新 更多