Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Reload Rows unnaturally jerking the TableView in iOS 11.2

Strange issue arrived after updating new XCODE 9.2 with iOS 11.2, by calling

let indexPath = IndexPath(item: 0, section: 1)
self.tableViewHome.reloadRows(at: [indexPath], with: .none)

It causes unnecessary jerking the whole TableView, Prior it was very fine with these code.

GIF Image for iOS 11.2

enter image description here

GIF Image for iOS 11.1

enter image description here

How to stop this Jerking, I have to reload only one cell in the tableview among two cells.

Any help will be appreciated.

Thanks

like image 554
Abhishek Mitra Avatar asked Dec 07 '17 08:12

Abhishek Mitra


1 Answers

What I understand is if you really want to use auto sizing cell(and all cell are of different sizes) then above solutions doesn't work for iOS 11.2, So what I tried is as below:

Declare variable to store the height of the cell

var cellHeightDictionary: NSMutableDictionary // To overcome the issue of iOS11.2

Initialize in viewDidLoad,

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 125
    cellHeightDictionary = NSMutableDictionary()
}

and use like below:

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

This works for me, Hope this helps to you too

like image 95
torap Avatar answered Oct 09 '22 19:10

torap