开发者

tableView sortDescriptorsDidChange: not getting called

Greetings,

I have an NSTableView with two columns that works fine... except: If I set the sort descriptor for the table in Interface Builder, things work as expected and sortDescriptorsDidChange gets called as expected. If, however, I don't set the sort descriptors in Interface Builder and instead use this:

[tableView setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:) ]autorelease]]];

(where "name" is the identifier for the leftmost column) in my code, sortDescriptorsDidChange never gets invoked. As I read (misread?) Apple's documentation for NSTableView, what I am doing, I think, should work. What am I doing wrong?

P.S. I k开发者_如何学Cnow that I could also use a NSArrayController for all this (and if I do it works fine), but for whatever reason, I have chosen not to.


it should work without this but have you tried sending the KVO notifications yourself ?

[tableView willChangeValueForKey:@"sortDescriptors"];
[tableView setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:) ]autorelease]]];
[tableView didChangeValueForKey:@"sortDescriptors"]; 

if you do not want to add the descriptors in IB you can do something like this

- (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];
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜