Error: UITableView jump to top with UITableViewAutomaticDimension

前端 未结 7 1741
半阙折子戏
半阙折子戏 2020-12-05 15:17

I am using UITableView with estimatedRowHeight and UITableViewAutomaticDimension. Also I am using NSFetchedResultsControllerDele

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 15:20

    To solve my problem I save the cell height in the willDisplay method and in estimatedHeightForRowAt I'm retrieving the value.

    var cellHeights = NSMutableDictionary()
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        if let height = cellHeights.object(forKey: indexPath) {
            return height as! CGFloat
        }
    
        return UITableViewAutomaticDimension
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cellHeights.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
    }
    

提交回复
热议问题