Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the reasons for subclassing NSArrayController?

I am trying to improve my KVC/KVO/Cocoa-Bindings-fu and was wondering what could be the reasons to subclass the NSArrayController?

like image 873
Eimantas Avatar asked Apr 03 '11 06:04

Eimantas


2 Answers

I have a custom NSArrayController subclass that perform a whole bunch of task. I chose to implement these things there because I can then enjoy the full comfort of bindings and stuff. Here's what I use this now for:

  • Sometimes some items must be hidden and some must be shown
  • I perform custom sorting (i.e. grouping) in the controller
  • It is fed by the items of a different kind than it returns (get items, returns item nodes - dummy objects that forward most stuff)
  • I also use it to hold the filter criteria and search options currently on display
  • In addition, I added NSTableView delegate and data source support that allows drag & drop implementation right in the controller
  • I also customize tool tips for the cell in there

Yeah, and so on. Basically this all boils down to this essence: subclass NSArrayController if you want different data out than you put in

Max

like image 150
Max Seelemann Avatar answered Sep 19 '22 16:09

Max Seelemann


One thing I like to do when using an array controller with a table view is to override add: to post a notification so that the new item is selected and open for editing right away. I actually posted this over at CocoaDev a while ago:

// Subclass of NSArrayController

- (void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter] addObserver: self 
                                             selector: @selector(objectAdded:) 
                                                 name: @"Object Added" 
                                               object: self]
}

- (void)add: (id)sender
{
    [super add: sender]
    NSNotification * note = [NSNotification 
                                notificationWithName: @"Object Added" 
                                              object: self]
    // The add method doesn't really take effect until this run loop ends,
    // (see NSArrayController docs) so the notification needs 
    // to wait to post. Thus, enqueue with NSPostWhenIdle
    [[NSNotificationQueue defaultQueue] enqueueNotification: note
                                               postingStyle: NSPostWhenIdle]
}

- (void)objectAdded: (NSNotification *)note
{
    // when the notification finally arrives, tell the table to edit
    [[self contentTable] editColumn:0 
                                    row:[self selectionIndex] 
                              withEvent:nil 
                                 select:YES]
}

Of course it's possible to do similar with a controller that's not an NSArrayController subclass; this is just the first way I figured out.

like image 20
jscs Avatar answered Sep 21 '22 16:09

jscs