简体   繁体   中英

Setting text and image on NSTableCellView

Hello I'm trying to use an NSTableView in my program and I'm having a problem setting the values for the NSTableCellView and getting them to display in the NSTableView. When I run my program, only blank cells show up. Using NSLog's, I can see that the cell imageView gets set, but doesn't display. When I go to set stringValues for the NSTableCellViews however, I only get null from my NSLog's despite the string containing data. Here's the delegate method I'm having a problem with:

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    NSString *cellIdentifier;
    NSImageView *pageImageView;
    NSString *pageString;

    int pageVotes;

    if (_connectionArray.count == 0) {
        return nil;
    }

    NSTableCellView *cellView = [[NSTableCellView alloc] init];

    if (tableColumn == tableView.tableColumns[0]) {
        cellIdentifier = @"firstColumn";
        pageImageView = [[_connectionArray objectAtIndex:row] getImage]; //Gets imageView from Page Object
        cellView.imageView = pageImageView; //Set image view for cell
        NSLog(@"%@", cellView.imageView); //This works
    }
    if (tableColumn == tableView.tableColumns[1]) {
        cellIdentifier = @"secondColumn";
        pageString = [[_connectionArray objectAtIndex:row] getTitle];
        cellView.textField.stringValue = pageString; //Set text for cell
        NSLog(@"%@", cellView.textField.stringValue); //Does not work, returns null
    }
    if (tableColumn == tableView.tableColumns[2]) {
        cellIdentifier = @"thirdColumn";
        pageVotes = [[_connectionArray objectAtIndex:row] getVotes];
        pageString = [NSString stringWithFormat:@"%i", pageVotes]; //Convert int to string
        cellView.textField.stringValue = pageString; //Set text for cell. 
        NSLog(@"%@", cellView.textField.stringValue); //Does not work, returns null
    }

    [_tableView makeViewWithIdentifier:cellIdentifier owner:self];

    return cellView;

}

I think everything set-up correctly between the Storyboard and the ViewController as well, but I could very well be wrong since this is my first time working with NSTableViews. I've also tried using:

[cellView setImage:pageImageView];
[cellView setTextField:[NSTextField textFieldWithString:pageString]];

but I run into the same issue. If anyone can help I greatly appreciate it! I feel like I'm missing something simple...

Setting the textField and imageView properties of NSTableCellView does not add a text field or an image view to the cell view. Those outlets are just intended to inform the cell view about which of its subviews are the primary text field and/or primary image view. You are still responsible for adding those views to the cell view as subviews or, possibly, as deeper descendant views.

Also, it's a bad idea for your model to vend views. That's not how it should work. Among other things, that will specifically interfere with adding those views to the cell view's subview hierarchy.

It's also strange that you're both creating the cell view and asking the table view to make it (by calling -makeViewWithIdentifier:owner: ). Normally, you'd do one or the other, or first try -makeViewWithIdentifier:owner: and only create a view if that fails. And, of course, you wouldn't ignore the return value.

Frankly, the best thing to do is set this all up in Interface Builder. If you do it right, there's no need to implement -tableView:viewForTableColumn:row: at all. Is there a reason you didn't go that route?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM