Objective c tableView SortDescriptorsIDChange:未被调用

Objective c tableView SortDescriptorsIDChange:未被调用,objective-c,cocoa,sorting,nstableview,Objective C,Cocoa,Sorting,Nstableview,您好 我有一个NSTableView,有两列,运行良好。。。除了:如果我在Interface Builder中为表设置排序描述符,事情会按预期进行,并且会按预期调用SortDescriptorsIDChange。但是,如果我没有在Interface Builder中设置排序描述符,而是使用以下选项: [tableView setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"nam

您好

我有一个NSTableView,有两列,运行良好。。。除了:如果我在Interface Builder中为表设置排序描述符,事情会按预期进行,并且会按预期调用SortDescriptorsIDChange。但是,如果我没有在Interface Builder中设置排序描述符,而是使用以下选项:

[tableView setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:) ]autorelease]]];
(其中“name”是最左边列的标识符)在我的代码中,永远不会调用SortDescriptorsIDChange。当我阅读(误读?)苹果的NSTableView文档时,我认为我所做的应该是可行的。我做错了什么


另外,我知道我也可以使用NSArrayController来完成所有这一切(如果我这么做了,它工作得很好),但出于任何原因,我选择不这样做。

如果没有它,它应该可以工作,但您是否尝试过自己发送KVO通知

[tableView willChangeValueForKey:@"sortDescriptors"];
[tableView setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:) ]autorelease]]];
[tableView didChangeValueForKey:@"sortDescriptors"]; 
如果您不想在IB中添加描述符,您可以这样做

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(NSInteger)rowIndex {
    //if we have no sort descriptor for this column create one based on it's identifier (instead of setting it for each in IB,saves time and prevents errors)
    NSSortDescriptor *desc = [theColumn sortDescriptorPrototype];
    if ([desc key] == nil) {
        NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:[theColumn identifier] ascending:YES];  
        [theColumn setSortDescriptorPrototype:sorter];
        [sorter release];
    }
}

您在什么时候调用
设置端口描述符
?根据我的经验,当对象(在您的例子中是
tableView
)没有实例化时,我调用它太早了(比如从nib中唤醒),可能就是这样。我正在调用WindowControllerIDloadNib中的setSortDescriptors(我可能错误地认为这是在基于文档的应用程序中执行此类操作的正确位置。)我刚刚检查,调用[myTableView setSortDescriptors]后,[myTabeView sortDescriptors]返回零。哪里(何时)是打此类电话的正确地点(时间)(或者我应该问一个关于窗口启动时发生的事情的顺序的新问题吗?)谢谢!后续:我将对SetSortDescriptor的调用移动到按钮按下方法中。以编程方式调用SetSortDescriptor确实会导致调用SortDescriptorsIDChange,但单击表列标题仍然不会。我是否还需要设置其他操作(单击列标题的操作或其他操作)?通过阅读文档,我认为这样的事情是不必要的。请检查
myTableView
是否为
nil
。这在
awakeFromNib
windowcontrolleridloadnib:
中都应该是正确的;前者发生在连接完所有连接后,后者发生在nib完全加载后(即,所有连接都已连接好)。好的…(1)在WindowControllerdLoadNIB中:tableView为零。(2)我添加了一个按钮来切换排序顺序(模拟我希望单击tableView列标题所做的操作),此时:(a)tableView为非零,并且(b)使用我在原始问题中发布的代码设置排序顺序的效果与预期一样,导致调用SortDescriptorsDidChange:。但是,实际上单击列标题仍然没有任何作用。到目前为止,我能够通过单击列标题进行表排序的唯一方法是在Interface Builder中指定它。