Make the last row in UITableView appear at top


Make the last row in UITableView appear at top



I have a UITableView with a random number of custom cells. It's working fine.



I am moving the selected cell to the top of the UITableView with:


tableView.scrollTo(index path animated)



However, when the selected cell is on the last full 'page' of cells, this doesn't work - obviously.



Can I make this work by somehow 'padding' out the table with empty space, dependent on the Y position of the selected cell, so that even selecting the last row, it would animate and move to the top of the table view?



I thought:


let point = CGPoint(x: 0, y: 200)
tableView.setContentOffset(point, animated: false)



would work, but it didn't (the 200) was a test amount.



I'm sure this is possible, could I please get a pointer?




2 Answers
2



If you want to allow a table view to be scrolled far enough for the last row to reach the top of the screen, set a view as the table view's tableFooterView.


tableFooterView



Make it an empty UIView with a height that is equal to the visible height of the table view minus the height of one row.


UIView





That's kinda clever. Can this be added dynamically? i.e. when the user is scrolling the table but hasn't selected a cell, then the last cell is at the bottom, but when a user touches a cell to select it, then I add in this 'footerView' and if the selected cell is the last or in the last page of cells, the whole table will grow enough that when I call 'scrollTo()' there will be space below the last cell?
– Elphaba
Jun 29 at 19:20





It should work. Give it a try.
– rmaddy
Jun 29 at 19:22





It works, the only problem I'm trying to nicely solve, is when the user starts scrolling the table again, to remove the footer, but without jerking the whole table offset from their finger's touchpoint. I'm guessing I'm going to have to programatically decrease the footers size during the 'scrollViewDidScroll' event, until the footer's bounds are 0 and then I can set the view to nil. but that seems messy.
– Elphaba
Jun 29 at 20:30




rMaddy gave the right answer. Thanks so much! I'd never have thought of using the footer!



Just for those that might be interested here was the working code:



NOTE, that it's just a prototype, so I'm hard coding number of cells and how many can be visible on screen at once.


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let maxNumberOfRows = 24
let maxNumberOfVisibleRows = 10
let safeArea: CGFloat = 20

let cell = tableView.cellForRow(at: indexPath)

if indexPath.row >= maxNumberOfRows - maxNumberOfVisibleRows
{
let numberOfSpacesRequired = ( maxNumberOfRows - indexPath.row ) * Int((cell?.bounds.height)!)

let spaceView = UIView()

spaceView.bounds = CGRect(x: 0, y: 0, width: Int(tableView.bounds.width), height: ( Int(tableView.bounds.height - safeArea) - numberOfSpacesRequired) )

tableView.tableFooterView = spaceView
}

tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift