Scroll in first table cell to center


I have a UITableView with custom cells. The cells are of a constant height 200. I want to animate the scrolling in of the first cell. This works using scrollToRowAtIndexPath. However, the row appears at the top of the view, whereas I would like it to appear dead center in the visible tableView. I tried setting the tableView's contentInset property to a bunch of different values, but none seem to provide the right offset. Any suggestions?


The only way I could find to do this was to use scrollRectToVisible instead of scrollToRowAtIndexPath. For whatever reason (possibly because scrolling on my table was disabled), the positioning was still off even using @Emil's suggestion of combining scrollToRowAtIndexPath with a contentInset.

This works:

tableView.contentInset = UIEdgeInsets(top: cellHeight, left: 0, bottom: cellHeight, right: 0)
tableView.scrollRectToVisible(CGRectMake(0, 0 - tableView.contentSize.height, cellWidth, tableView.contentSize.height), animated: false)

This video can help you solving your question :)
By: admin