After performBatch (or table reloadData) contentOffset reset to zero solution

Zion·2021년 9월 6일
1

출처

tableView performBath 이후에
scrollView contentOffset 이 쌩뚱맞은 위치에가있는다.

contentOffset.y가 0으로 reset되는데, 그 해결이다

var cellHeightsDictionary: NSMutableDictionary = [:]
...
// save height
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cellHeightsDictionary.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
}

// height value 복원
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if let height = cellHeightsDictionary.object(forKey: indexPath) as? Double {
        return CGFloat(height)
    }
    return UITableView.automaticDimension
}
profile
어제보다만 나아지는

0개의 댓글