使用动态递增单元格计算表格视图的内容大小



我正在尝试计算具有动态递增单元格的表格视图的内容大小高度。为此,我正在编写代码作为 -

override func viewDidLoad()
    {
        super.viewDidLoad()
        self.tableView.estimatedRowHeight = 150
        self.tableView.rowHeight = UITableViewAutomaticDimension
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {

            let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! MostLikedTVCscnd
            cell.image.image = imageArr[indexPath.row] as? UIImage
            cell.aboutLbl.text = labelArr[indexPath.row] as? String

let contentSizeHeight  = tableView.contentSize.height//But here I am not getting the proper height
return cell
}

我检查了计算表查看内容大小的问题中的答案 - https://stackoverflow.com/a/17939938/5395919 其内容如下

请注意,对于那些

不起作用的人 - tableView:estimatedHeightForRowAtIndexPath弄乱了contentSize,所以你可能只是通过删除它的实现来运气。

那么,尽管单元格大小是动态的,但有什么方法可以获得正确的内容大小吗?

您可以使用键值观察 (KVO) 在表视图的内容大小更改时收到通知。这可能适用于也可能不适用于您要做的事情。我只是不清楚你想获得什么,但我认为尝试在cellForRowAtIndexPath中使用它并不好。

最新更新