Skip to content

Instantly share code, notes, and snippets.

@cozzin
Last active August 19, 2018 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozzin/ae3a04b09e757914af0a7829260777da to your computer and use it in GitHub Desktop.
Save cozzin/ae3a04b09e757914af0a7829260777da to your computer and use it in GitHub Desktop.
How to cache height values ​​through delegate when using rxdatasoruce
class ViewController: UIViewController {
private var cellHeightsDictionary: [String: CGFloat] = [:]
override func viewDidLoad() {
super.viewDidLoad()
// set table view ...
tableView.rx.setDelegate(self).disposed(by: disposeBag)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeightsDictionary[indexPath.cacheKey] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeightsDictionary[indexPath.cacheKey] ?? UITableViewAutomaticDimension
}
}
private extension IndexPath {
var cacheKey: String {
return String(describing: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment