Search code examples
macoscocoanstableviewnstableviewcell

Multiple cells that don't exist on NSTableView


I have a NSTableView with 3 cells. I have adjusted that table view to show the separators, by using this code:

  [self.tableView setGridColor:[NSColor lightGrayColor]];
  [self.tableView setIntercellSpacing:NSMakeSize(1, 1)];
  [self.tableView setGridStyleMask:NSTableViewDashedHorizontalGridLineMask];

When I render the table I see this:

enter image description here

The table shows a number of cells that are not supposed to exist and with a different size!

Is this normal? How do I get rid of those "phantom cells"?


Solution

  • If all the content rows in your table are the same size, you can use the NSTableView size inspector to specify the size. If you do this, the system-added 'padding' rows will have this size also.

    enter image description here

    To get rid of the grid lines associated with the system-added rows, the snippet below worked for me in a very quick demo app (this snippet is not my own, it was taken from an answer to another Stackoverflow question)

    // MyNSTableView.m (only need to override this one NSTableView method)
    
    - (void)drawGridInClipRect:(NSRect)clipRect
    {
        NSRect lastRowRect = [self rectOfRow:[self numberOfRows]-1];
        NSRect myClipRect = NSMakeRect(0, 0, lastRowRect.size.width, NSMaxY(lastRowRect));
        NSRect finalClipRect = NSIntersectionRect(clipRect, myClipRect);
        [super drawGridInClipRect:finalClipRect];
    }
    

    Combining the two gives the following effect (I'm using alternating row colors to show that the gird lines are indeed not drawn for the system-added padding rows).

    enter image description here