Kodr.F
Kodr.F

Reputation: 14400

UITableView jumping on scrollToRow?

I am facing issue with UITableView in Swift. Table contains lyrics. Each cell is a lyric class. I know which index I have to focus on so I show the current lyric in red and the rest in black.

All okay so far. But when using scrollToRow, it's working well before table starts to scroll down to lyrics outside the view. Once the UITableView starts scrolling outside the view, it starts jumping.

Any idea how to make it scroll smoothly to the current cell without jumping?

Here is how I make the table go to the desired lyric:

let lcCell = IndexPath(row: (vc_lyrics.sharedInstance().lyric_time[seconds]?.index)!, section: 0)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
    vc_lyrics.sharedInstance().tableView.scrollToRow(at: lcCell, at: .none , animated: false)
    vc_lyrics.sharedInstance().tableView!.reloadData()
}

Or if I can keep the lyric cell in the center by scrolling the scrollview of the table, not its delegate scrollToRow.

Upvotes: 1

Views: 660

Answers (1)

Paolo
Paolo

Reputation: 3955

Change this line to set animated to true:

vc_lyrics.sharedInstance().tableView.scrollToRow(at: lcCell, at: .none , animated: true)

This will cause the cells to scroll smoothly. I would also think about whether or not you need to reload the tableView data here because that can also cause scrolling issues as @rmaddy mentioned.

Upvotes: 1

Related Questions