NSTableView scrolling not working

I have a problem with scrolling a table.

I dragged scrollview to xib with frame.

And I create the table view programmatically and add it to the scrollview as follows:

NSRect frame = [[ScrollView contentView] bounds]; rTableView = [[MTableView alloc] initWithFrame:frame]; 

MTableView is my custom class inheriting from NSTableView , and in the constructor I add columns to the table view.

 [ScrollView setDocumentView: resultsTableView]; [ScrollView setHasVerticalScroller:YES]; [ScrollView setHasHorizontalRuler:YES]; [rTableView setDelegate:self]; [rTableView setDataSource:self]; [ScrollView addSubview: rTableView]; [rTableView reloadData]; 

I wrote delegate functions to load data, and the table is populated with data. But the scroll does not work. I have 100 rows in a table and I can see the first 21 rows when the table view is loaded. And nothing happens with the scroll, and the row index (which is in data deletion) is always 19 or 20. I'm not sure what is going wrong here. I saw some old posts on stackoverflow following the NSTableView documentation, but didn't help much.

Any help is greatly appreciated.

+4
source share
1 answer

You are doing everything right, except for this line:

 [ScrollView addSubview: rTableView]; 

The standard layout of NSScrollView + NSTableView has NSTableView set as documentView for NSScrollView . You correctly configured these relationships programmatically in your code:

 [ScrollView setDocumentView: resultsTableView]; 

When you added the line [ScrollView addSubview: rTableView] , which added the table view in a way that was not intended. Basically, this addition serves only to ensure that you do not see a properly viewed table view that is below it.

Removing this line [ScrollView addSubview: rTableView] should allow you to see the table view correctly and let the scroll mechanisms work properly.

An NSScrollView has many subzones that make up the scroll architecture (see Scroll Programming Guide for Mac ). Examining the standard " NSTableView inside NSScrollView ", available in the Interface Builder palette, shows that there are 4 immediate sights in the scroll view:

 "<NSClipView: 0x103507200>", "<NSScroller: 0x101b14f80>", "<NSScroller: 0x101b15210>", "<NSClipView: 0x103507be0>" 

One of the clips contains an NSTableView , and the other NSTableHeaderView . Using NSScrollView setDocumentView: in the line [ScrollView setDocumentView: resultsTableView] correctly configure the table view as the corresponding view of the NSClipView document. On the other hand, adding a table view as a direct subset of NSScrollView will produce unexpected results that you see.

+1
source

All Articles