简体   繁体   English

使用 UITableViewAutomaticDimension 重新加载 tableview,Bounce

[英]Reload tableview with UITableViewAutomaticDimension , Bounce

Today I have gone through with the very varied behaviour of the table view.今天我已经经历了表格视图的非常不同的行为。 It's bounce every time I call reload function as my table view cells are dynamic and having a different type of contents, so I used the property of UITableViewAutomaticDimension for row height.每次我调用重新加载函数时它都会反弹,因为我的表格视图单元格是动态的并且具有不同类型的内容,所以我使用 UITableViewAutomaticDimension 的属性作为行高。

So, to resolve this bounce issue on the reload function of the table.因此,要解决表的重新加载功能上的此反弹问题。 I have to store the height of the row from tableView willDisplay cell method and used the same in estimatedHeightForRowAt.我必须从 tableView willDisplay cell 方法存储行的高度,并在estimatedHeightForRowAt 中使用相同的高度。 Please find the code below.请在下面找到代码。 Hope it will help someone.希望它会帮助某人。

var height_OfCells = NSMutableDictionary()

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if let height = height_OfCells.object(forKey: indexPath) {
        return height as! CGFloat
    }

    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    height_OfCells.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
}

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

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