How to make NSTableView scroll to most recently added row?

ruipacheco picture ruipacheco · Nov 25, 2009 · Viewed 11.5k times · Source

I'm dynamically adding rows to an NSTableView. When I issue [table reloadData] I can see the scroll view move and if I move it by hand I can see the new value on the table. But how can I scroll it automatically?

Answer

Wevah picture Wevah · Nov 25, 2009
NSInteger numberOfRows = [tableView numberOfRows];

if (numberOfRows > 0)
    [tableView scrollRowToVisible:numberOfRows - 1];

Assuming you're adding rows to the end of the table.

(The reason I ask the table view for the number of rows instead of the data source is that this number is guaranteed to be the number of rows it knows about and can scroll to.)