【问题标题】:Cannot get cell in tableview by heightForRowAt无法通过 heightForRowAt 获取表格视图中的单元格
【发布时间】:2018-06-24 23:15:01
【问题描述】:

我尝试为动态内容设置单元格高度。在单元格中,我使用了两个 UILabel,但我无法从 tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) 访问单元格。正如 tableview 显示的那样,img 中有单元格,但是当我在模拟器中运行应用程序时,print("Cell geted") 没有输出任何内容。 我只是IOS开发的初学者,所以我的应用程序中可能有一些错误的配置。我想知道什么原因会导致这种情况

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

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let cell = self.tableView.cellForRow(at: indexPath) as? ArticleTableViewCell
    if(cell != nil) {
        print("Cell geted")
    }
    return 120
}

我使用 xib 文件来定义单元格

Comtum 细胞类如下

import UIKit

class ArticleTableViewCell: UITableViewCell {

    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var digest: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        time.layer.borderWidth = 1
        //time.layer.masksToBounds = true
        time.layer.cornerRadius = 10
        time.backgroundColor = UIColor.cyan
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

故事板xib文件如下

【问题讨论】:

    标签: ios swift uitableview autolayout row-height


    【解决方案1】:

    heightForRow 中,您应该返回UITableViewAutomaticDimension。如果你这样做了,那么tableView 在从cellForRowAt 获取单元格时会自动使用自动布局。

    简而言之:

    1. viewDidLoad 设置中:

      tableView.estimatedRowHeight = 100
      tableView.rowHeight = UITableViewAutomaticDimension
      
    2. 在单元格中设置自动布局,以便单元格可以根据其内容确定自己的大小。

      要使用自动布局在 xib 单元格中设置单元格设计,请阅读 this article。在您的情况下,您可以执行以下操作:

      首先,单击标签将其选中。然后点击右下角的“Add New Constraints”图标,见下图:

      一个小弹出窗口显示,其中设置了一些常量,这些常量定义了标签应该被限制在距离单元格的顶部、左侧、底部和右侧多远的位置。确保指示正在激活的约束的红线在实线上(一旦您填写常量,它们应该从虚线变为实线)。先尝试为每个方向设置 8,以后你可以玩它。

    However, make sure that you read [this article][1] and also [this article on autolayout][4].
    
    1. 只需在cellForRowAt 中返回一个单元格。 TableView 将使用单元格的自动布局来确定其高度。

    详情请见this answer,或我的other answer for an example in code

    编辑

    要使单元格根据行数展开,在didSelect中将标签的行数设置为0,并使用以下方法告诉tableView重绘自己:

    func refreshTableAfterCellExpansion() {
        self.tableView.beginUpdates()
        self.tableView.setNeedsDisplay()
        self.tableView.endUpdates()
    }
    

    didDeselect 中做同样的事情,只是颠倒过来。

    【讨论】:

    • 在我的代码中,我不知道为什么这对我不起作用。如果我在tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) 中使用return UITableViewAutomaticDimension,标签将重叠在一起。而且似乎单元格高度太小了。
    • 您在单元格中的约束不好,您还必须将标签约束到左、右、上和下.. 我猜您没有将标签约束到底部
    • 我不知道如何将标签相互约束,所以我只是将标签放在情节提要中而没有约束配置。我想这可能是原因
    • @KrisRoofe 分享您设计单元格的代码.. 还是您使用故事板来定义其设计?
    • 我想进一步提高动态高度。起初我只想为摘要标签保留 4 行。所以我设置cell.digest.numberOfLines = 4,当我选择单元格时,所选单元格中的摘要标签会展开到全高。如果我取消选择单元格,标签将缩小到 4 行。我怎样才能做到。谢谢。
    【解决方案2】:

    您必须提供数据的高度(通过计算您的数据所具有的任何度量),您无法访问该委托方法中的单元格。

    【讨论】:

      【解决方案3】:

      要设置行高和估计行高的自动尺寸,请确保执行以下步骤,自动尺寸对单元格/行高布局有效。我刚刚测试了以下步骤和代码并且工作正常。

      • 分配和实现 tableview 数据源和委托
      • UITableViewAutomaticDimension 分配给rowHeight 和estimatedRowHeight
      • 实现委托/数据源方法(即heightForRowAt并返回值UITableViewAutomaticDimension给它)

      -

      @IBOutlet weak var table: UITableView!
      
      override func viewDidLoad() {
          super.viewDidLoad()
      
          // Don't forget to set dataSource and delegate for table
          table.dataSource = self
          table.delegate = self
      
          // Set automatic dimensions for row height
          table.rowHeight = UITableViewAutomaticDimension
          table.estimatedRowHeight = UITableViewAutomaticDimension
      }
      
      
      
      // UITableViewAutomaticDimension calculates height of label contents/text
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return UITableViewAutomaticDimension
      }
      

      对于 UITableviewCell 中的标签实例

      • 设置行数=0(&换行模式=截尾)
      • 设置与其父视图/单元格容器相关的所有约束(上、下、左右)。
      • 可选:设置标签的最小高度,如果你想要标签覆盖的最小垂直区域,即使没有数据。

      注意:如果您有多个标签(UIElements)具有动态长度,应根据其内容大小进行调整:为您的标签调整'Content Hugging and Compression Resistance Priority'想要以更高的优先级展开/压缩。

      在此示例中,我设置了低拥抱和高抗压优先级,这导致为第二个(黄色)标签的内容设置更高的优先级/重要性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多