Scroll NSTableView to selection
Asked Answered
B

1

8

OK, this is my situation :

  • I have an NSMutableArray bound to an NSTableView, via an NSArrayController.
  • I'm manipulating the table view's selection (trigger by keyboard), using NSArrayController's selectNext: and selectPrevious:

The thing is : The selection does change, but the table view doesn't scroll to the current selection, so that its content is visible.

What should I do? Any ideas?

Baumgardner answered 8/3, 2013 at 11:28 Comment(0)
W
19

There is a method in NSTableView

- (void)scrollRowToVisible:(NSInteger)row;

That will scroll to the specified row.

Weld answered 8/3, 2013 at 11:48 Comment(3)
Thanks for the suggestion. I've already tried it and it works. However, there is still some subtle issue : a) although I'm doing [myTableView scrollRowToVisible:[myTableView selectedRow]] (right after selectNext: and selectPrevious:, it doesn't scroll to the current line, but the one either before or after it (respectively). b) even after a [myTableView scrollRowToVisible:[myTableView selectedRow]+1] (after selectNext:), it does show up, but scrolling is not smooth. (HINT : The table view is not in focus). Any idea?Baumgardner
Sorry for late reply, actually I created a project to check, and it worked for me, as I used - (IBAction)button:(id)sender { [self.array addObject:@"Anoop"]; [self.myTable reloadData]; [self.myTable scrollRowToVisible:self.array.count-1]; }Weld
Had the same issue as @Dr.Kameleon. I guess it's because when you invoke selectPrevious, the view's properties will not immediately change. Dispatching scrollRowToVisible for "later" helped, too. It's not pretty, but it's less of a hack to mitigate the race conditions than +/-1: DispatchQueue.main.async { tableView.scrollRowToVisible(tableView.selectedRow) }Sabrasabre

© 2022 - 2024 — McMap. All rights reserved.