Table View - How to tap a cell and automatically move the next cell to the top?

In my Table View, I have a list of instructions (1 cell = 1 instruction). I am trying to make it so that:

  1. User taps on the top cell
  2. The screen automatically scrolls so that the 2nd cell is now on the top

Example:

Before tapping the first cell:

After tapping the first cell:

I’ve been researching, and a lot of people seem to use a removeObjectAtIndex command to remove the cell that was tapped, but in my case:

  1. The user should still be able to scroll down to reveal that tapped cell again, so it’s not technically removed, but moved out of the view until the user wants to move it back into view
  2. Only the next index can be tapped to reveal this functionality

Would someone be able to point me to the right direction to a method that could accomplish this? I’ve experienced similar in many apps so I feel like something must exist out there to use. I would think this method would be nested within func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath).

I’ve been thinking that the defaulted top cell would always satisfy both of the following conditions:

  1. Cell has not been tapped
  2. Lowest index count

Any help/guidance is much appreciated!

Found a couple more links that might be helpful. Will post updates on this thread :slight_smile:


Solved :slight_smile: This worked:

currentInstructionIndex += 1
        let indexPathTest = IndexPath(row: currentInstructionIndex, section: 0)
        tableView.scrollToRow(at: indexPathTest, at: UITableView.ScrollPosition.top, animated: true)

The tableView.scrollToRow method is what I was looking for. More info here for anyone who’s interested: https://developer.apple.com/documentation/uikit/uitableview/scrollposition

1 Like