Reputation: 365
I have tableview and some of the cells are off screen. I am working on letting a user reorder the list but sometimes the current one is then off screen. A rough example would be how emails works. If you are on a message and resort by sender instead of date, the message you are currently stays on the list of messages.
In swift is there a way for me to scroll the table view down or something so that a certain cell is visible on screen? Right now I only know how to resort the data array and reload the table... what I want is to have a certain cell guaranteed on screen after the reloaded table.
If I were to construct this as an exercise I would present an array of 10 names with a 1-10 id where only 5 can be on screen. Once a tableview cell is tapped the id for "currentlySelected" is saved. Place 2 buttons at the top where sorts by name and <123> by id
If I selected 2-steve and then clicked the ABC button... how can I make it so that 2-steve is on the screen since only 5 names can show on the screen at a time and steve is ABC ordered as position 7?
<ABC> <123>
1-dan
2-steve
3-ted
4-carol
5-anne
6-tony
7-linda
8-chris
9-blake
10- tom
Upvotes: 0
Views: 65
Reputation: 4277
If you know the Index Path of the Cell you want to scroll to, just use the Table View provided method:
if let indexPath = tableView.indexPathForSelectedRow {
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
Or, generally:
let indexPath = IndexPath(row: 1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
Note: the row should be a value in [0...9] range in your case.
Upvotes: 1