adding a string value to an NSTableView

Discussion in 'Mac Programming' started by Chirone, Jun 18, 2009.

  1. macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #1
    ...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
     
  2. macrumors member

    Joined:
    Jun 4, 2009
    #2
    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
     
  3. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #3
    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
     
  4. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #4
    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:

    Code:
    - (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).
     
  5. thread starter macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #5
    awesome, i'll give that a try when i get the chance
     

Share This Page