NSButton in NSTableCellView: How to find desired objectValue?
Asked Answered
S

2

5

I have a view-based NSTableView that is populated through bindings. My textFields & imageViews are bound to the NSTableCellView's objectValue's properties.

If I want to have an edit/info button in my NSTableCellView:

  1. Who should be the target of the button's action?

  2. How would the target get the objectValue that is associated with the cell that the button is in?

I'd ultimately like to show a popover/sheet based on the objectValue.

Shrier answered 17/2, 2012 at 21:4 Comment(0)
A
7

Your controller class can be the target. To get the object value:

- (IBAction)showPopover:(id)sender {
    NSButton *button = (NSButton *)sender;
    id representedObject = [(NSTableCellView *)[button superview] objectValue];
}

Or, use a subclass of NSTableCellView, make the cell view the target of the button's action, and call [self objectValue] to get the object.

Auditory answered 22/2, 2012 at 18:24 Comment(0)
H
15

I found an additional answer: The Answer above seems to assume you're using bindings on your table view. Since I'm kind of a noob I found a way to get the button inside the table view cell.

- (IBAction)getCellButton:(id)sender {
     int row = [xmlTable rowForView:sender];
}

This way when you click on the button inside the row, you don't have to have the row selected. It will return the int value of the row to match up with a datasource in an array without bindings.

Huntington answered 14/5, 2012 at 1:43 Comment(1)
... and if you want to control the appearance of the button per row, you can bind its title and image property to Table Cell View objectValue properties. Just not the button's state property, unfortunately.Elvaelvah
A
7

Your controller class can be the target. To get the object value:

- (IBAction)showPopover:(id)sender {
    NSButton *button = (NSButton *)sender;
    id representedObject = [(NSTableCellView *)[button superview] objectValue];
}

Or, use a subclass of NSTableCellView, make the cell view the target of the button's action, and call [self objectValue] to get the object.

Auditory answered 22/2, 2012 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.