NSTableView first column is too large

Discussion in 'Mac Programming' started by davbeck, Oct 10, 2008.

  1. davbeck macrumors regular

    Joined:
    May 16, 2008
    Location:
    Reno, NV
    #1
    I have a program, that displays the contents of a csv file (soon to also allow mysql). First I just had it displaying the data, but then I went and added an NSPopUpButtonCell, at the top of each row with the delegate method:
    Code:
    - (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex {
    	if(rowIndex == 0) {
    		NSPopUpButtonCell *cell = [[[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO] autorelease];
    		[cell setEditable:YES];	[cell setBordered:NO];
    		[cell addItemsWithTitles:[NSArray arrayWithObjects:@"Data", @"Start X", @"Start Y", @"Start Z", @"End X", @"End Y", @"End Z", @"Ignore", nil]];
    		return cell;
    	} else {
    		NSTextFieldCell *cell = [[[NSTextFieldCell alloc] initTextCell:@""] autorelease];
    		[cell setEditable:YES];
    		return cell;
    	}
    }
    When I added that, the first column takes up the entire row, no matter what size the row, or the columns, it expands to the entire table, covering the other columns. all the columns are probably the same size (they all have the pop up) but are being covered by the first column.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Don't use this method for setting a custom cell because you're creating a new cell for each row, which is wasteful. The preferred way is to create the cell during awakeFromNib, and assign it to your table column. Something like (untested):
    Code:
    NSTableColumn *column = [tableView tableColumnWithIdentifier:@"Column1"];
    NSPopUpButtonCell *cell = [[[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO] autorelease];
    [cell setEditable:YES];
    [cell setBordered:NO];
    [column setDataCell:cell];
    Then implement tableView:willDisplayCell:forTableColumn:row:

    Edit: I didn't realize you're using a different cell for different rows, which is strange. If you want to do this, I'd cache the cells in the init method and return them in this method, instead of creating new ones every time.
     
  3. davbeck thread starter macrumors regular

    Joined:
    May 16, 2008
    Location:
    Reno, NV
    #3
    Thanks for the help.
    As far as the full width cells go, You have to return nil when given a nil column. Otherwise it will interpret it as being a single cell for the entire row.
     

Share This Page