1
votes

I have situation where I use view based Table Views and don't want to use bindings between data source and table view. This is mainly due to the fact that my NSTableCellView can have multiple subviews, complex validation and triggered calls to methods in other objects.

We have very clear path of updating NSTableView with data source with:

tableView:viewForTableColumn:row:

However, for backwards, that is updating datasource with updates in NSTableView we have nothing of the sort we have for cell based Table views:

tableView:setObjectValue:forTableColumn:row:

Target Action pattern is suggested instead. So, I have basically 2 questions:

  1. If I set target and action for one specific view, or its subview, how do I get proper row and column info to know what to update in data source?

Should clickedRow and clickedColumn from NSTableView do the trick, although I have edited or changed one subview object?

  1. How can I inform the target (as other object, not NSTableView instance) about row and column if action will pass for example NSTextField as parameter?

I can basically come to clickedColumn and clickedRow (if those 2 properties are proper answer to first question) through subview tree, but I find this as pretty much non-elegant solution and have hunch there is a better way....

THanks in advance....

2

2 Answers

2
votes

NSTableCellView has an objectValue. Presumably you're already setting it, so the action can use [(NSTableCellView *)[sender superview] objectValue] to find out which object it needs to manipulate.

I suggest that you also subclass NSTableCellView and implement the action there. If you need access to other parts of the model, you can add an outlet for your view controller.

If you really need the row number, you can call indexOfObject on your content array.

1
votes

The two NSTableView Methods rowForView and columnForView should do the trick. You can call them with the sender of an Target/Action Method, like one triggered by an NSButton in your TableView (its ok, to have it somewhere in a subwiew) Or you can call these Methods from within a delegate method implementation like the textDidChange from NSTextDelegate. So you can easily update your corresponding Array. If you don't want continous updates textDidEndEditing would also do the job.

- (void)textDidChange:(NSNotification *)notification
{
    NSTextView *tv = [notification object];
    int r = [tableView rowForView:tv];
    int c = [tableView columnForView:tv];
    NSLog(@"Row: %d Column: %d", r, c);
    // updating code here
}