0

Is there a simple way to bind a NSMutableArray made up of Strings to a Single column NSTableView without creating any new classes?

2 Answers2

2

Make an NSArrayController in interface builder. Bind its Content Array to your mutable array, like so:

Binding

Then bind your table column to that array controller, like so:

Binding2

(Note the self in the model key path field. That's what makes it work with an array of strings.)

Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
  • Hmmm. I hope i am missing something. @property (nonatomic, retain) NSMutableArray *stringArray; is what i have in appDelegate.h so i used stringArray instead of array in Pic 1. Still i don't see the NSTableView getting populated. – user3193123 Jan 22 '14 at 18:25
  • It worked in a test project. Take a look at this: https://dl.dropboxusercontent.com/u/18362750/bindings-test.zip – Amy Worrall Jan 23 '14 at 12:42
  • Note that if you do this, you'll need to make the table view read-only. If you want to allow editing your strings, you really should be making your own model class and binding to that. – Amy Worrall Jan 23 '14 at 12:44
  • Perfect. Thanks. The code sample cleared me. I was trying with mutable array. – user3193123 Jan 23 '14 at 17:31
  • I have to use an mutable array - the length of the array might change but not it's string contents. So creating a model class is the only way to bind? – user3193123 Jan 23 '14 at 20:53
1

Assuming you're using a view-based NSTableView, you must bind the entire table view (not the column) to the array controller's arrangedObjects. Then, bind the text field's value to the containing cell view's objectValue property.

This is documented here.

For cell-based NSTableViews, Amy Worrall's answer is the correct approach.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97