子类化NSArrayController的原因是什么?

Eim*_*tas 15 cocoa subclass nsarraycontroller cocoa-bindings

我正在努力改进我的KVC/KVO/Cocoa-Bindings-fu,并想知道继承NSArrayController的原因是什么?

Max*_*ann 17

我有一个自定义NSArrayController子类,执行一大堆任务.我选择在那里实现这些东西,因为我可以享受绑定和东西的完全舒适.以下是我现在使用的内容:

  • 有时必须隐藏某些项目,并且必须显示一些项目
  • 我在控制器中执行自定义排序(即分组)
  • 它由与其返回的不同类型的项目提供(获取项目,返回项目节点 - 转发大多数内容的虚拟对象)
  • 我还用它来保存当前显示的过滤条件和搜索选项
  • 此外,我添加了NSTableView委托和数据源支持,允许在控制器中实现拖放实现
  • 我还为那里的单元格定制了工具提示

是的,依此类推.基本上这一切都归结为这个本质:子类NSArrayController,如果你想要不同的数据输出

马克斯


Jos*_*ell 9

使用带有表视图的数组控制器时,我喜欢做的一件事是覆盖add:以发布通知,以便选择新项并立即打开进行编辑.我实际上刚刚在CocoaDev发布了这个:

// 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]
}
Run Code Online (Sandbox Code Playgroud)

当然,可以与不是NSArrayController子类的控制器类似; 这只是我想出的第一种方式.