在调整某些项目之前,自定义单元格无法正常工作

时间:2017-03-17 08:15:23

标签: ios swift uitableview

我使用自我调整大小UITableViewCell,其中包含一些标签,所有标签行号都是0。

tableView.estimatedRowHeight = 284
tableView.rowHeight = UITableViewAutomaticDimension

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell", for: indexPath) as! NodeCell
    let nodemodel = nodes[indexPath.row]
    cell.like.text = Utils.convert_string(string: String(nodemodel.like))
    cell.address.text = nodemodel.address
    cell.avatar.downloadedFrom(link: "www.example.com")
    cell.avatar.layer.cornerRadius = cell.avatar.frame.size.width / 2
    cell.avatar.layer.borderWidth = CGFloat(integerLiteral: 1)
    cell.avatar.layer.borderColor = UIColor.black.cgColor
    cell.avatar.clipsToBounds = true
    cell.title.text = nodemodel.node_name
    cell.layoutIfNeeded()
    return cell
}

运行应用程序后,我的所有标签都限制为单行和文本没有完全显示,直到滚动一些项目然后向后滚动。怎么了?在返回单元格之前我做了layoutIfNeeded()但它没有工作 enter image description here

2 个答案:

答案 0 :(得分:1)

UIStackView分发模式从Fill Proportionally更改为Equal Spacing后,

问题得以解决

答案 1 :(得分:0)

请尝试使用UITableView delegate方法:

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

    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 284
    }
相关问题