2

I would like to bind NSTableColumn's headerTitle property to an NSMutableArray in my model layer (via an NSArrayController).

Basically I want to have an array where I can change values and have the table column header titles update. Is that reasonable?

However, the headerTitle binding wants an single NSString and I'm not sure how to connect my model object to this binding via my NSArrayController. Google does not give many hits for this problem.

My model layer consists of two class (both of which are appropriately KVC compliant). The first is a model which represents a single column title, it has one property title,

// A model class representing the column title of single NSTableColumn
@interface ColumnTitle : NSObject
@property NSString *title;
+ (ColumnTitle*) columnTitleWithTitle:(NSString*) aString;
@end

The second a model object which represents an ordered group of ColumnTitle objects,

// Class representing an order collection of model items
@interface TableColumnTitles : NSObject
@property NSMutableArray* columnTitles;  // an array of ColumnTitle objects
// These are the KVC array accessors
-(void) insertObject:(ColumnTitle*)columnTitle inColumnTitlesAtIndex:(NSUInteger)index;
- (void)removeObjectFromColumnTitlesAtIndex:(NSUInteger)index;
- (void)replaceObjectInColumnTitlesAtIndex:(NSUInteger)index withObject:(ColumnTitle*)columnTitle;
@end

Note that TableColumnTitles object implements the above array accessors which are required for the bindings. Any suggestions?

Daniel Farrell
  • 9,316
  • 8
  • 39
  • 62

2 Answers2

1

Haven't tried that before but what you're actually asking for is using KVC for array indexes. A quick google didn't turn up anything on that issue except some results that indicate it's not (yet) possible (check this)

The easiest work-around I could come up with would be to simply add dedicated properties for the array indexes.. not nice but does the job.

So for a NSMutableArray called myArray and contains objects with title properties of type NSString you'd do something like:

@property (nonatomic, readonly, getter = columnOneGetter) NSString *columnOneString;

(NSString*) columnOneGetter
{
   return myArray[0].title;
}

Always assuming of course their number is known in advance and we're not talking 200 columns :-)

Community
  • 1
  • 1
Jay
  • 6,572
  • 3
  • 37
  • 65
  • I'm just getting started with binds and I'm not sure how to go about implementing your idea. Could you explain in a bit more detail. Also, in general, how would you go about using the `NSTableColumn`'s `headerTitle` binding? Am I using it in an unusual way? I basically want to be able to set the table header titles by changing values in an array controller; it doesn't sound too controversial, but I can't find an example of how to do this anywhere! – Daniel Farrell Feb 16 '14 at 09:01
  • Answer edited accordingly - usual disclaimer: typed into browser, not tested nor debugged, etc. – Jay Feb 16 '14 at 10:06
  • It does sound a bit weird to bind the current selection to the header, at least I can't come up with a use case where you'd want to change the header when the selected row in the table changes. – Jay Feb 16 '14 at 10:08
  • Actually I don't want to bind to the selection, that was just an example. The way I wrote the question is a bit confusing so sorry for that. I basically want to have a model `NSMutableArray` that I allows me to modify the header titles. That's it. – Daniel Farrell Feb 16 '14 at 10:30
0

I think this may/may not be what you're after, but quick google search landed me here: http://pinkstone.co.uk/how-to-add-touch-events-to-a-uitableviewfooter-or-header/

edit: i realize this is for mac (not ios) but should be pretty easy to translate if it actually helps.

Jonathon Hibbard
  • 1,547
  • 13
  • 20