From c6e2e867e156beb6461cc33419a26717e8d18cca Mon Sep 17 00:00:00 2001 From: CMK Date: Sat, 3 Jul 2021 03:31:49 +0800 Subject: [PATCH] chore: add not stable cell height cache back --- .../NotificationViewController.swift | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/Mastodon/Scene/Notification/NotificationViewController.swift b/Mastodon/Scene/Notification/NotificationViewController.swift index 9772f516..f2de1d80 100644 --- a/Mastodon/Scene/Notification/NotificationViewController.swift +++ b/Mastodon/Scene/Notification/NotificationViewController.swift @@ -189,33 +189,33 @@ extension NotificationViewController { extension NotificationViewController: StatusTableViewControllerAspect { } // MARK: - TableViewCellHeightCacheableContainer -//extension NotificationViewController: TableViewCellHeightCacheableContainer { -// var cellFrameCache: NSCache { -// viewModel.cellFrameCache -// } -// -// func cacheTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { -// guard let diffableDataSource = viewModel.diffableDataSource else { return } -// guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return } -// let key = item.hashValue -// let frame = cell.frame -// viewModel.cellFrameCache.setObject(NSValue(cgRect: frame), forKey: NSNumber(value: key)) -// } -// -// func handleTableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { -// guard let diffableDataSource = viewModel.diffableDataSource else { return UITableView.automaticDimension } -// guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return UITableView.automaticDimension } -// guard let frame = viewModel.cellFrameCache.object(forKey: NSNumber(value: item.hashValue))?.cgRectValue else { -// if case .bottomLoader = item { -// return TimelineLoaderTableViewCell.cellHeight -// } else { -// return UITableView.automaticDimension -// } -// } -// -// return ceil(frame.height) -// } -//} +extension NotificationViewController: TableViewCellHeightCacheableContainer { + var cellFrameCache: NSCache { + viewModel.cellFrameCache + } + + func cacheTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { + guard let diffableDataSource = viewModel.diffableDataSource else { return } + guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return } + let key = item.hashValue + let frame = cell.frame + viewModel.cellFrameCache.setObject(NSValue(cgRect: frame), forKey: NSNumber(value: key)) + } + + func handleTableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { + guard let diffableDataSource = viewModel.diffableDataSource else { return UITableView.automaticDimension } + guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return UITableView.automaticDimension } + guard let frame = viewModel.cellFrameCache.object(forKey: NSNumber(value: item.hashValue))?.cgRectValue else { + if case .bottomLoader = item { + return TimelineLoaderTableViewCell.cellHeight + } else { + return UITableView.automaticDimension + } + } + + return ceil(frame.height) + } +} // MARK: - UITableViewDelegate