On Apr 2, 2008, at 12:35 AM, Thomas Bartelmess wrote:
i've created a NSTableview with Interface Builder. In one of my Collums is a NSButton. I've no idea how to set up the DataSource for this.

An NSButton as in a checkbox? I've just recently coded 2 tables with this, so I might be able to shed some light. If you're using something other than a checkbox, this won't apply directly, but maybe you can glean something from it.

You need to implement 3 data source methods:

1. - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView

2. - (void)tableView:(NSTableView *)aTableView
      setObjectValue:(id)anObject
      forTableColumn:(NSTableColumn *)aTableColumn
                 row:(NSInteger)rowIndex

3. - (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
               row:(NSInteger)rowIndex

You need to maintain some sort of data model that represents each row; in my case, I have a checkbox column, and a "name" column. I maintain the list of names in an NSArray, ordered appropriately. I also maintain an NSMutableSet; if a corresponding object from the name array is present in the set, that means the checkbox for that row is set, otherwise it's clear. So, for the three methods above:

1. return the count of items in the array

2. "anObject" is a NSCFBoolean (NSNumber subclass?) for the checkbox column, and an NSString for the name column; in my case, the name column is not editable, so I don't worry about this at all (if I did, then I'd have to use a NSMutableArray and change the name at index "rowIndex" to the "anObject" value). For my checkbox column, I get the BOOL value out of "anObject", and if it's YES then I add the object at index "rowIndex" of my name array to the set, otherwise I remove it from the set.

3. For my checkbox column, I return an NSNumber object containing a BOOL value: YES if the name at index "rowIndex" of my name array is in my set, and NO otherwise. For the name column, I simply return the name at that index in my name array.

May not be the cleanest way in the world to do it, but it works well for my needs (which don't involve very large amounts of data in my table).
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to