Re: Calling tableView:setObjectValue directly...
Re: Calling tableView:setObjectValue directly...
- Subject: Re: Calling tableView:setObjectValue directly...
- From: Fritz Anderson <email@hidden>
- Date: Mon, 8 Mar 2004 14:44:23 -0600
You're fighting the framework if you think in terms of NSTableView
maintaining row and column data. Whatever object you provide as the
TableView's data source maintains the data; the view just asks for it,
and notifies the data source when the user makes changes. NSTableView,
as we programmers are to use it, knows nothing about the data it
displays.
It's therefore a mistake to look for an NSTableView method to
programmatically set the value for a particular cell. Whenever you know
your data source will report a different value for a cell, send
'reloadData' to the TableView to make it pull the new data into the
view.
You say you have one non-editable column that depends solely on the
value of an editable column. You also have a "recalculate" button to
trigger the change in the dependent column. Unless the calculation is
very long, or has side-effects, this is unnecessary. You can
recalculate the non-editable value whenever the editable value changes;
or even calculate it only when the table asks for it. That way, the
user doesn't have to remember to push the button to update the display.
To beat my point utterly to death (I was curious to see if you had to
call reloadData inside tableView:setObjectValue:forTableColumn:row: --
you don't), here's a dumb data source that takes a number in an
editable column, and displays its double in an uneditable one:
@interface DoubleController : NSObject {
IBOutlet NSTableColumn * singleColumn;
IBOutlet NSTableColumn * doubleColumn;
IBOutlet NSTableView * doubleTable;
NSMutableArray * contents;
}
- (IBAction) addRow: (id) sender;
@end
@implementation DoubleController
- (void) awakeFromNib {
contents = [[NSMutableArray alloc] init];
[doubleTable reloadData];
} // dealloc omitted
- (IBAction) addRow: (id) sender {
[contents addObject: [NSNumber numberWithInt: 1+[contents count]]];
[doubleTable reloadData];
}
#pragma mark NSTableDataSource
- (int) numberOfRowsInTableView: (NSTableView *) tableView { return
[contents count]; }
- (id) tableView: (NSTableView *) tableView objectValueForTableColumn:
(NSTableColumn *) tableColumn row: (int) row {
NSNumber * value = [contents objectAtIndex: row];
if (tableColumn == singleColumn)
return value;
else
return [NSNumber numberWithInt: 2 * [value intValue]];
}
- (void) tableView: (NSTableView *) tableView setObjectValue: (id)
object forTableColumn: (NSTableColumn *) tableColumn row: (int) row {
[contents replaceObjectAtIndex: row withObject: object];
}
@end
On 8 Mar 2004, at 12:43 PM, Peter.Teeson wrote:
I have a Document app that has a TableView and have implemented the
usual the functions for the informal protocol NSTableDataSource and
they get called when I do the normal sorts of editing on the table
itself.
However one of the columns in my table is deliberately made not
editable. The reason is that it is a calculated value based on what
has been entered in a different column.
I have a Button that invokes code to generate the values for the 'not
editable' column, on a cell by cell basis, and I had thought at first
to try calling [myTable setObjectValue......] but I get compilation
warnings on this telling me it can't find the function and that it may
not respond to the message.
Yet they are there and are being called when editing other columns,
presumably from other methods of NSTableView (at least that's what the
debug stack shows).
So why am I not allowed to call them directly? Or if I am can someone
please suggest what I have misunderstood? Preferably with a code
snippet.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.