PDA

View Full Version : adding a string value to an NSTableView




Chirone
Jun 18, 2009, 11:12 PM
...so i'm having trouble trying to add values from an array to an NSTableView

i could use bindings but the code doesn't allow for such things to exist. that and i even if i could get the key i wouldn't know how to get the value i want because it's a 2D array i'm reading off

anyway, what's the method that will add a string value to an nstable?
sounds like it should be simple... just like a addValue:atColumn:atRow method...

i've searched the internet for adding values to nstableviews but nothing useful comes up...

ok, nevermind, it'd not actually possible says the documentation. guess i will have to try the popup button instead



cqexbesd
Jun 19, 2009, 03:52 AM
I believe you are thinking about it the wrong way. Rather than the table view holding the data you need to provide a delegate that the tableview will ask for data when it needs it.

Hope that helps - the docs should take you further - I don't have them to hand atm

Chirone
Jun 19, 2009, 05:19 AM
yeah, i looked at the delegates but didn't see one, maybe i still managed to miss the correct delegate

i found that you can get the table to read it's data off a so-called dataSource, so maybe, next time i'll do that

but for now the popup button achieves the outcome that i wanted, and for what it's doing it was probably a better choice

GorillaPaws
Jun 19, 2009, 09:21 AM
The way it works is that you declare the class you're using to manage the data for that table, the "data source" of that table (by wiring it up in IB). then you need to implement 2 methods if it's a non-editable table to something similar to this:

- (int) numberOfRowsInTableView: (NSTableView *) aTableView
{
return [anArrayOfData count];
}

- (id) tableView: (NSTableView *) aTableView
objectValueForTableColumn: (NSTableColumn *) aTableColumn
row: (int) rowIndex
{
NSString *s = [anArrayOfData objectAtIndex: rowIndex];
return s;
}


The table decides when it's time to call those methods (your data source class is it's helper).

Chirone
Jun 19, 2009, 09:34 AM
awesome, i'll give that a try when i get the chance