0
votes

I have a source list view in my app that's set up to be view based. I then added a badge/button to my cells via this method;

First subclass NSTableCellView, add an IBOutlet for an NSButton and a @synthesize/@property statement to it. Then open the NSTableCellView that should have a badge in Interface Builder. Set it's class to your newly created subclass and add a button to it. Set the button style to "inline" and it's type to "switch".

Now select the NSTableCellView again and connect the NSButton IBOutlet to your added button. That's it. You can now call e.g. [[cellView button] setTitle@"123"]] to set the rows badge label to 123 or any arbitrary string.

This worked great, but clicking the badge doesn't actually select that row, so I added a click handler to the button.

- (IBAction)sourceListBadgeClicked:(id)sender {
    NSUInteger selectedRow = [self.outlineView rowForView:sender];
    if (selectedRow == -1) {
        return;
    }

    NSLog(@"click row: %lu", selectedRow);

    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:1];
    [self.outlineView selectRowIndexes:indexSet byExtendingSelection:NO];
}

I can see the handler get called, but selectedRow is always returned as 0, and selectRowIndexes doesn't actually select the row (I hard-coded to 1 just to test).. Is there something I'm missing in how to do this?

1

1 Answers

1
votes

Turns out it really helps to bind the IBOutlet for the view you're trying to manipulate.. Rookie mistake on my part.