objective-ccocoanstableviewappkit

Can't change the font size of an NSTableView


I'm following this code here where I have an NSTableView that displays the folder name as a group header and then the contents of the folder below, for this example these are all images.

table

So like, here I have a folder named flags and a folder named states, what I want to do is to change the font size of this header, I have successfully changed the text color but for some reason I can't change the font size neither through storyboard nor programmatically. Here's my tableView:viewForTableColumn:row: method

 -(NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
{
    
    DesktopEntity *entity = _tableContents[row];
    
    
    if ([entity isKindOfClass:[DesktopFolderEntity class]])
    {
        NSTextField *groupCell = [tableView makeViewWithIdentifier:@"GroupCell" owner:self];
        [groupCell setStringValue: entity.name];

        [groupCell setFont:[NSFont fontWithName:@"Arial" size:40]];
        [groupCell setTextColor:[NSColor magentaColor]];
        return groupCell;
    }
    else if ([entity isKindOfClass:[DesktopImageEntity class]])
    {
        NSTableCellView *cellView = [tableView makeViewWithIdentifier:@"ImageCell" owner:self];
        [cellView.textField setStringValue: entity.name];
        [cellView.textField setFont:[NSFont fontWithName:@"Impact" size:20]];
        [cellView.imageView setImage: [(DesktopImageEntity *)entity image]];
        return cellView;
    }
    
    return nil;
}

what am I doing wrong here, and how can I change the font size programmatically?

Edit

I tried changing the font size in the second if statement for the "ImageCell" and it works, but I still can't get it to work for the "GroupCell" in the first if statement.

Looking a bit further, there's this method tableView:isGroupRow: that I'm using:

-(BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row
{
    DesktopEntity *entity = _tableContents[row];
    
    if ([entity isKindOfClass:[DesktopFolderEntity class]])
    {
        return YES;
    }
    
    return NO;
}

I found out that if I comment out this function those changes work and the font size gets updated, but after removing this function, the cell won't act like a group cell but rather as a normal cell which is not what I wanted. Any way around this?

Here's the table after trying to change the font size and color(while still maintaining the isGroupRow method), you can see that the font color changed but its size remains the same, what gives?

enter image description here


Solution

  • As Willeke explained, the solution here is to subclass NSTableRowView and override the isGroupRowStyle method

    @interface CustomTableRowView : NSTableRowView
    
    @end
    
    @implementation CustomTableRowView
    
    -(BOOL)isGroupRowStyle
    {
        return NO;
    }
    
     //custom background for a table row view
    - (void)drawBackgroundInRect:(NSRect)dirtyRect
    {
    
        NSColor *groupBackgroundColor = [NSColor windowBackgroundColor];
        [groupBackgroundColor setFill];
        NSRectFill(dirtyRect);
    
    }
    
    @end
    

    I also had to override rowViewForRow for the NSTableViewDelegate:

    - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
    {
        DesktopEntity *entity = _tableContents[row];
        
        if ([entity isKindOfClass:[DesktopFolderEntity class]])
        {
            return [[CustomTableRowView alloc] init];
        }
        
        // use default row for non-group rows
        return nil;
    }