Last active
August 19, 2018 14:44
-
-
Save cozzin/ae3a04b09e757914af0a7829260777da to your computer and use it in GitHub Desktop.
How to cache height values through delegate when using rxdatasoruce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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