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?

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223

1 Answers1

19

There is a method in NSTableView

- (void)scrollRowToVisible:(NSInteger)row;

That will scroll to the specified row.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • 1
    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? – Dr.Kameleon Mar 08 '13 at 11:58
  • 1
    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]; }` – Anoop Vaidya Mar 08 '13 at 12:20
  • 1
    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) }` – ctietze Jan 05 '17 at 09:17