UITableView Scrolls to Top on Cell Refresh

后端 未结 4 1842
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 10:08

I have a UITableView with a cell that is dynamically sized to fit a UITextView inside it. Whenever a key is typed, the cell checks to see if the calculated height has increased,

4条回答
  •  情歌与酒
    2021-02-04 10:13

    More simplified solution (add Notification observer when textView is changed), Swift-version. Main trick is using UIView.setAnimationsEnabled.

    func textViewDidChanged(notification: NSNotification) {
        UIView.setAnimationsEnabled(false)
    
        let contentOffset = self.tableView.contentOffset
    
        tableView.beginUpdates()
        tableView.endUpdates()
    
        tableView.contentOffset = contentOffset
    
        UIView.setAnimationsEnabled(true)
    }
    

提交回复
热议问题