0

I've created a subclass of NSScrollView which is set as the class of a scroll view with table view in IB. I increased the height of the table's header view and added a label to act as the title for the table view. The problem is that the table rows start below the column headers by an amount that is equal to the amount that I made the header view taller (so the column titles appear in the middle of the new taller header view).

enter image description here

I've tried changing the origin or size of the table, the table's clip view, the header view, and its clip view -- none of this changes anything except for the size of the header view. This is the code I used to make the custom header view:

-(void)awakeFromNib {
    //self is a subclass of NSScrollView enclosing an NSTableView
    NSClipView *tableClip = [self.subviews objectAtIndex:0];
    NSClipView *headerClip = [self.subviews objectAtIndex:3];
    NSTableHeaderView *header = [[[self.subviews objectAtIndex:3] subviews]lastObject];
    NSTableView *table = [[[self.subviews objectAtIndex:0] subviews]lastObject];
    NSLog(@"%@  %@  %@  %@",tableClip,headerClip,header,table);

    [header setFrameSize:NSMakeSize(header.frame.size.width, header.frame.size.height+60)];//HeaderView Frame
    NSTextField *titleLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 5, header.frame.size.width,22)];
    titleLabel.selectable = NO;
    titleLabel.font = [NSFont systemFontOfSize:16];
    titleLabel.alignment = NSCenterTextAlignment;
    titleLabel.bordered = NO;
    titleLabel.stringValue = @"This is my title";
    [header addSubview:titleLabel];
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • What are you trying to do here? Since you already have the NSTableView inside of an NSScrollView, why not just put your title above the NSTableView (using an NSTextView or NSTextField) and then leave the header of the NSTableView alone? Also, why are you subclassing NSScrollView in the first place, it looks like you could do most of this in the XIB by embedding the top label and the table in the NSScrollView. – gaige Jun 01 '12 at 11:44
  • @gaige I'm doing it to try to achieve a more integrated look. When you select the table the focus ring, will go around the whole thing, including the title, which it won't do if you add a title above the table. As far as doing it in IB, I don't know a way to do that, which would have the title not scroll, which is what happens if you embed a table and label in a scroll view. – rdelmar Jun 01 '12 at 15:10
  • I misunderstood your comment above about having subclassed the NSScrollView, and "self is a subclass of NSScrollView enclosing an NSTableView". You might look at subclassing NSTableHeaderView, although I'm not sure what is involved in that through experience, a google search indicates it is possible. – gaige Jun 01 '12 at 15:15
  • Just a note: this is very fragile code. You should not refer to subviews by index number, and certainly not assume that the order will be the same in future releases. Create/set properties and bind them in the xib (or programmatically) as needed. Take advantage of properties like `enclosingScrollView` found on `NSView`, `headerView` on `NSTableView`, `contentView` from `NSScrollView` (to get the clip view), and others that are similar. – Demitri Oct 19 '15 at 00:49

0 Answers0