简体   繁体   English

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

[英]Calculating content Size of Table View with dynamic increasing cell

I am trying to calculate the tableView's content size height which has dynamic increasing cells. 我正在尝试计算tableView的内容大小高度,它具有动态增加的单元格。 For that I am writing code as - 为此,我正在编写代码 -

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

}

I checked an answer in the question for calculating tableView content size - https://stackoverflow.com/a/17939938/5395919 which reads 我在计算tableView内容大小的问题中检查了一个答案 - https://stackoverflow.com/a/17939938/5395919

Note for those for whom this isn't working -- tableView:estimatedHeightForRowAtIndexPath messes with contentSize, so you might have luck just by removing your implementation of it. 注意那些不能使用它的人 - tableView:estimatedHeightForRowAtIndexPath混淆了contentSize,所以你可以通过删除你的实现来获得运气。 – weienw - weienw

So is there any means in which I can get correct content size despite dynamic cell size? 那么,尽管有动态单元格大小,我还能获得正确的内容大小吗?

You can use key value observing (KVO) to be notified whenever the table view's content size changes. 只要表视图的内容大小发生变化,您就可以使用键值观察(KVO)进行通知。 This may or may not work for what you're trying to do. 这可能会或可能不适用于您正在尝试的操作。 I'm just not clear on what you're looking to obtain, but I don't think it would be good to try to use that in cellForRowAtIndexPath. 我只是不清楚你想要获得什么,但我认为尝试在cellForRowAtIndexPath中使用它并不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM