Search code examples
objective-ccocoanstableviewnsimage

Can't create an NSImage?


Whenever I do:

xxx = [NSImage imageNamed:@"Package.png"];

xxx loads but it's width and height remain 0. And whenever I try loading it into an NSImageCell I get this error:

NSImageCell's object value must be an NSImage.

Can someone help me out? I've never had this problem before.

Edit: Sorry, I've missed this bit out. So when I do it in the data source delegate it does not work and it shows the above error after 'return cell;'.

NSImageCell* cell = [[NSImageCell alloc] init];
[cell setObjectValue:xxx]; // Using imageNamed doesn't help either

Edit 2: This is becoming aggravating. I don't understand what happened but the image loads height and width properly, but it still complains when I add it to an NSImageCell.

alt text


Solution

  • I see—so the table view aspect is relevant after all.

    As the data source, your job is to return (and receive, in the case of user editing) the object values for the cells in the columns.

    But you're not returning such a value; you're returning a cell. Thus, you're trying to set an image cell (created by the data source) as the value of an image cell (the existing one owned by the column).

    The log message suggests that you have already set the column's cell as an image cell when you created the column, so all you need to do now is change your data source to always return the object value for the column, not a cell. For an image column, return the image. For a text column, return the string.

    Note that NSTableView does not work like UITableView, where UITableViewCells are UIViews and you have as many cells as rows on the screen; in NSTableView, each NSTableColumn gets one and only one data cell, and that one cell is used to draw that column of every row. The cell draws its object value, which you provide to the cell, which you do by returning it (the object value) from your data source method.

    The documentation about controls (an NSTableView is a kind of NSControl) and their cells is the Control and Cell Programming Guide.