如何在swit中设置自定义UITabelview单元格的动态高度

时间:2016-09-29 16:56:08

标签: iphone uitableview row-height

我有一个UITableView,其中填充了由Xib(自动布局)创建的自定义单元格。单元格由多个标签图像等组成,大小根据我们从api获得的响应而变化。我一直在努力设置高度动态。我正在使用以下方法。

func createTabelView ()
    {
    let nib = UINib(nibName: "DBEventCell", bundle: nil)
        self.eventTabelVew.registerNib(nib, forCellReuseIdentifier: Constants.CELL_IDENTIFIER)

        self.eventTabelVew.estimatedRowHeight = 200
        self.eventTabelVew.rowHeight = UITableViewAutomaticDimension
        self.eventTabelVew.separatorStyle = .None //UITableViewCellSeparatorStyle.SingleLine;
        self.eventTabelVew.separatorColor = UIColor.clearColor();
        self.eventTabelVew.backgroundColor = UIColor.whiteColor();

        self.eventTabelVew.setNeedsLayout()
        self.eventTabelVew.layoutIfNeeded()
    }


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

        return UITableViewAutomaticDimension
    }

但这不起作用。请帮助我,因为我从最近几天开始受到打击。  提前致谢

1 个答案:

答案 0 :(得分:0)

我认为你不需要这个功能

 func tableView(tleView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return UITableViewAutomaticDimension
}

你也应该检查你的单元格autolayout约束和调整UI组件的属性以使其与内容签约或扩展也检查本教程

https://www.appcoda.com/self-sizing-cells/