如何使用Swift创建具有动态单元格高度的静态单元格

时间:2016-08-04 23:21:17

标签: ios swift xcode7.3

我已经看了几个教程,展示了如何设置动态单元格高度,但是如果您通过设置适当的约束并使用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()中的两行。这样做对我来说!请记住,我必须确保每个单元格中的所有标签都使用顶部,底部,前导和尾随约束。此外,每个标签的行数必须设置为0。

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

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

5 个答案:

答案 0 :(得分:8)

您正在使用每个单元格的文本视图。好的,让我们选择您的文本视图并取消选中启用滚动,如下图所示。

enter image description here

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

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

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

答案 1 :(得分:5)

对于Swift 3

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

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

答案 2 :(得分:2)

您只需在AutoLayout内设置正确的cell,因此根据说明文字的长度增加高度。  然后,您需要在viewDidLoad

中设置以下内容
tableview.rowHeight = UITableViewAutomaticDimension
tableview.estimatedRowHeight = 44

请注意:您需要将numberOfLines的{​​{1}}设置为0

答案 3 :(得分:1)

@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 :(得分:0)

@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
}
相关问题