16

I have simple chat application with text messages view-based NSTableView as you can see at the picture below. enter image description here Each message contains NSTextView instance having height to fit all the text.

All I need is to start NSScrollView (which NSTableView-instance is enclosed by) autoscrolling while the user selecting text dragging mouse far enough. Unfortunately, autoscrolling doesn't appear. In case of dragging somewhere outside of the text views all succeed.

I tried to call autoscroll:-method directly by simply push NSEvent-instance from NSTextView-subclass "mouse dragged"-event (like in example from this article):

- (void)mouseDragged:(NSEvent *)event
{
  [self.scrollView autoscroll:event];
}

As I've overrode all the mouse events and implemented all the text selecting, this method often invokes. But the autoscrolling doesn't seem to work.

UPDATE

I figured out that before calling -autoscroll:-method there must be -mouseDown: of the same object. But it breaks my text selecting mechanism. The point even not in being first responder, there must be nothing but the mouseDown:-method.

Daniyar
  • 2,975
  • 2
  • 26
  • 39
  • If you are saying that with an NSTableView inside an NSScrollView and the NSTableView rows are multi-line, and when selecting text inside a cell and dragging down below the bottom of the scroll view, it does not scroll? If so, that functionality is provided automatically by NSScrollView and nothing else is needed for scrolling to occur while selecting. To test, I created a new project, added an NSScrollView (NSTableView), set row height larger than the scroll view and populated the tableview. Clicking on text and then selecting down past the bottom of the scrollview. It scrolled automatically. – Jay May 28 '15 at 17:41
  • @Jay I need to start NSTableView scroll view autoscrolling, not the text view's one (the text view has height enough to fit the text). – Daniyar May 29 '15 at 06:44
  • I am not sure what you are asking. Assuming that you drug an NSTableView from the Objects Library onto the window in Xcode, the NSTableView will be enclosed by an NSScrollview. If you are trying to select the text in a row in a tableview by clicking the mouse in the row where the text is and dragging down (which is how it appears from the screen shot, the enclosing NScrollView handles that automatically and will scroll up so you can continue to select the text. – Jay May 29 '15 at 17:14
  • @Jay when I'm selecting the text It starts the NSTextView's scrollView autoscrolling. But I want to autoscroll the tableView's scroll view. – Daniyar May 29 '15 at 17:54
  • I think it should be scrolling automatically. In the test project I created, the tableView height was 100 and I set the row height to 200 and the cellView height to 200 and filled each row with enough text to make it go beyond the height of the tableview. When I select the text in a row and drag down the entire table scrolls. – Jay May 29 '15 at 19:56
  • But it doesn't scroll automatically since I overrode all the mouse events in NSTextView-subclass just to achieve sequential text view selecting through the tableview. – Daniyar May 30 '15 at 05:05

1 Answers1

2

Normally, a text view is within a scroll view of its own. Even if that's big enough to show all of the text without scrolling, it's still there. A call of -autoscroll: on anything within that scroll view (possibly including that scroll view itself?) will just try to scroll that scroll view, not the scroll view that contains the table view.

Try calling -autoscroll: on a view higher up in the hierarchy. Either self.scrollView.superview, the table cell view, or the table view.

Note, though, that the table view's scroll view will keep scrolling even after the cell view containing the text view is fully on-screen. In fact, it may keep scrolling it so far that it's off the screen in the other direction. Basically, it doesn't know that you're trying to select within the text view so it doesn't know to stop when the selection extends all the way to the edge of the text view.

Another approach might be to try to use a "bare" text view with no enclosing scroll view. I don't think IB will let you do that, so you'd have to do it programmatically. Bare text views don't play well with auto layout, though.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Calling `-autoscroll:` (on table view or its enclosing scroll view) with an event taken from `-mouseDragged:` parameter causes nothing. May be I should mutate `NSEvent` somehow. – Daniyar Jun 09 '15 at 10:20