3

I need to implement a headerview with specific size and gradient. I have to insert images in certain cells of the headerview.Tried to create the cells for the headerview using the following code,but i was not able to customize the headerview.

[[tableColumn headerCell] setImage:[NSImage imageNamed:@"sampleHeader"]];

If I use the overridden subclass of headerview, I was not able to view the images or text in the header cell.Please provide me any pointers to solve this issue.

 

I was able to insert images and text by subclassing the NSTableHeaderCell.How to increase height of the NSTableHeaderView?


If I subclass both NSTableHeaderView and NSTableHeaderCell , was not able to view anything in the headercell.I used the following code for setting headerview and headercell

[tableView setHeaderView:CustomHeaderView];

[tableColumn setHeaderCell:[[[CustomHeaderTableCell alloc] initImageCell: [NSImage imageNamed:@"sample"]]autorelease]];

I have the same issue as given in the below url

http://lists.apple.com/archives/cocoa-dev/2002/Jun/msg00331.html

RobEarl
  • 7,862
  • 6
  • 35
  • 50
Ram
  • 1,872
  • 5
  • 31
  • 54

3 Answers3

14

You don't need to subclass NSTableHeaderView.

I was able to change the height of the header view using the following snippet in the controller class:

-(void)awakeFromNib {
    NSRect frame = tableView.headerView.frame;
    frame.size.height = 26;
    tableView.headerView.frame = frame;
}

It should be noted that the scroll view takes care of the layout. It automatically changes the frame of the headerView as necessary, but leaves the height intact. Resizing the clip view etc as suggested in the other answer is not necessary.

Jakob Egger
  • 11,981
  • 4
  • 38
  • 48
  • 1
    This doesn't fully answer the situation. Following this snippet I can see the NSTableHeaderView indeed becomes higher --- but the column header cells are drawn at the original standard height, leaving a blank table-wide bar on top of them. So, it is not exactly true that layout is automatic. Maybe you need to tell somebody else to re-layout the column header cells according to their new TableHeaderView size. – Motti Shneor Jul 10 '14 at 07:23
  • The layout IS automatic, but the problem is that NSTableHeaderCell draws its background with a fixed height. You need to subclass it and override `-drawWithFrame:inView:`. The simplest implementation would be to draw a gradient as background and then call `-drawInteriorWithFrame:inView:` – Jakob Egger Jul 15 '14 at 19:03
  • But the problem isn't just with the background! If I put more text there, and configure the cell (NSTableHeaderCell) to wrap text -- it doesn't wrap. Rather I only see the beginning of the first line. I was afraid the cell is not given the correct (higher) frame to draw itself in. Otherwise --- why didn't it draw the text correctly? – Motti Shneor Jul 17 '14 at 21:00
  • NSTableHeaderCell doesn't draw wrapped text. You need to subclass it and override `-drawWithFrame:inView:`. – Jakob Egger Jul 23 '14 at 08:17
  • This didn't work reliably for me until I followed it with `[tableView tile]`. – JWWalker Apr 05 '19 at 00:40
2

You can also create a NSTableHeaderView object, initialize it with a frame(rect with height and width) and set that NSTableHeaderView object to your table view.

 NSTableHeaderView *tableHeaderView = [[NSTableHeaderView alloc] initWithFrame:NSMakeRect(0, 0, 120, 60)];
    [myTableView setHeaderView:tableHeaderView];
[tableHeaderView release];
triandicAnt
  • 1,328
  • 2
  • 15
  • 40
1

Following link helped me in solving the issue.

http://lists.apple.com/archives/cocoa-dev/2003/Feb/msg00676.html

You need to set the Frame for NSClipView, NSTableHeaderView and the CornerView This is how I implemented the same in Code.

for(NSView * subview in [topScrollView subviews])
{           
   for(NSView * subSubView in [subview subviews])
   {
      if([[subSubView  className] isEqualToString:@"NSTableHeaderView"] &&  [[subview className] isEqualToString:@"NSClipView"]) 
      {
         [subSubView setFrameSize:NSMakeSize(subSubView.frame.size.width, subSubView.frame.size.height+5)];//HeaderView Frame
         [subview setFrameSize:NSMakeSize(subview.frame.size.width, subview.frame.size.height+5)];//ClipView Frame
      }

    }
    if ([[subview className] isEqualToString:@"_NSCornerView"])
    {
       [subview setFrameSize:NSMakeSize(subview.frame.size.width, subview.frame.size.height+5)]; //CornerView Frame
    }
}
Ram
  • 1,872
  • 5
  • 31
  • 54
  • 1
    I am not sure what properties were available in appkit back in 2011 but nowdays you certainly don't need a double for loop to get the table header view, or the clipview or the cornerview... – Radu Simionescu Feb 12 '15 at 16:09