How to make NSTableView scroll to most recently added row?
12,081
Solution 1
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.)
Solution 2
Use something like this:
[table scrollRowToVisible:indexOfNewRow];
Solution 3
I solved it as shown below, using the didAddRowView to work properly: Obviously, if I add a new record, the tableview needs to create a new row view.
- (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row {
// it sends notification from all tableviews.
// I am only interested in the dictionary...
if (tableView == dictTableView){
if ( row == [dictTableView numberOfRows]-1){
[dictTableView scrollRowToVisible:row];
}
}
}
But also, in my IBAction I added 2 lines after performing the add statement to the controller
- (IBAction)addNewDictItem:(id)sender {
NSInteger row=[[theDictArrayController arrangedObjects]count];
[theDictArrayController add:sender];
[dictTableView noteNumberOfRowsChanged];
[dictTableView scrollRowToVisible:row-1];
}
Related videos on Youtube
Author by
ruipacheco
Updated on March 20, 2020Comments
-
ruipacheco about 4 years
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?
-
Abizern over 14 years+1 because this way handles the usual case where table sorting means that the new row isn't necessarily at the bottom of the table.
-
SpacyRicochet about 10 yearsSomehow (on the latest XCode and SDK) this causes the tableView scroll to the cell just before the newly added cell (added at the end of the tableView). Can't figure out why.
-
Chris Hillery about 10 yearsIt might depend on how soon you call it; maybe try adding
[tableView noteNumberOfRowsChanged];
before it. -
SpacyRicochet about 10 yearsThanks, will try that. Right now I got it working by putting
[tableView reloadData]
in front of it.