Posts

Post not yet marked as solved
7 Replies
4.3k Views
I have a UITableView which hold various Settings for an app, including Location. If the user taps on this, there's a segue on the Storyboard that moves to a new UITableViewController which lists all the countries, in sections according to their inital letters. Before the segue, the original TableViewController passes the new TableViewController the index of the currenttly chosen location. I'd like the new TVC to scroll so that this location is at the top when the new TVC is presented.I created a dummy app with 300 rows and no sections. The 'scrollToRow' function works fine with the vast majority of cases. But if I pass the row 282 as the index, it comes a cropper. It scrolls to row 282, but doesn't move it to the top. It moves it close to the top. The bottom row visible is row 298, rather than 299, so it could clearly scroll up more. I have no idea why it doesn't.I call the 'scrollToRow' in my 'viewDidLoad' of the new TVC.var selectedIndex: IndexPath? override func viewDidLoad() { super.viewDidLoad() self.tableView.layoutIfNeeded() self.tableView.scrollToRow(at: selectedIndex!, at: .top, animated: false) }I've also tried putting the lines in the original TVC...override func prepare(for segue: UIStoryboardSeghue, sender: Any?) { if segue.identifier == "SegueTest" { if let test2TVC = segue.destination as? Test2TVC { let selectedIndex = IndexPath(row: 282, section: 0) test2TVC.selectedIndex = selectedIndex test2TVC.tableView.layoutIfNeeded() test2TVC.tableView.scrollToRow(at: selectedIndex, at: .top, animated: false) } } }But sadly, this made no difference. I must be doing something wrong as this can clearly be achieved - it's exactly what Apple do in their Settings app on the iPhone. Or perhaps there's a clever work-around.Any ideas extremely welcome!Thanks
Posted
by AidanK.
Last updated
.