iTunes的计数按钮使用Cocoa绑定 [英] iTunes-like count buttons using Cocoa bindings

查看:156
本文介绍了iTunes的计数按钮使用Cocoa绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在侧栏中显示一些项目,每个标签的计数也会显示:

I want to display some items in my sidebar, with the count of each tag also displayed:

如何有效地自动执行?简单的选择是使用可可绑定,但我不知道什么最好的方式做到这一点:每个按钮需要它自己的NSArrayController与fetch谓词设置为'标签'?这可能会最终与X数目的NSArrayControllers(每个标签一个),这将是相当重量(我会认为)。

How do I do this efficiently and automatically? The easy option would be to use cocoa bindings, but I'm not sure what the best way to do this: would each button needs it's own NSArrayController with a fetch predicate set for the 'tag'? That could end up with X number of NSArrayControllers (one for each tag) which would be pretty heavy-weight (I would think).

另一个选项是手动创建获取请求,然后对受管对象上下文中的每个更改进行重新获取。但这似乎有点凌乱,不自动。

The other option is to create fetch requests manually, then refetch for every change in managed object context. But that seems a bit messy and not-as-automatic.

有没有更简单的解决方案?我已经google了,没有找到任何东西。

Is there a simpler solution for this? I've googled around and haven't found anything.

推荐答案

让我们假设你的NSOutlineView中有一个childrenKeyPathchildren,孩子们有一个boolean isNew属性。你想要的是一个对象类的单元格视图中一个很好的 numberOfNewItems 气泡。我会称之为父对象。

Let's assume that in your NSOutlineView you have a childrenKeyPath of "children" and the children have a boolean isNew attribute. What you want is a nice numberOfNewItems bubble in the cell view for one object class. I'll call that the parent object.

如果你只是想在childrenKeyPath中的对象数量,heck,这更容易,但我会带你通过

If you just want the number of objects in the childrenKeyPath, heck, that's even easier, but I'll take you through the more complex case of tracking a specific boolean property on the children because it's easy enough to simplify this pattern.

在父对象的表单元格视图中添加 objectValue.numberOfNewItems 隐藏绑定到标题 > objectValue.NumberOfNewItems 与值transform NSNegateBoolean。如果你只想要孩子的数量,切换那些键值到 objectValue.children.count ,你就完成了。如果你想跟踪一个属性,如isNew,让我们继续...

In the table cell view for the parent object add a recessed button with the title bound to objectValue.numberOfNewItems and hidden bound to objectValue.NumberOfNewItems with value transformer NSNegateBoolean. If you just want the number of children, swith those keypaths to objectValue.children.count and you're done. If you want to track a property like isNew, let's continue...

在父对象类中,有这样的代码:

In the parent object class, there is this code:

- (NSNumber*) numberOfNewItems
{
     // Collection operator on boolean returns total number of new children
     return [self valueForKeyPath:@"children.@sum.isNew"];
}

// This setter does nothing, but with KVO it causes bindings 
//  to numberOfNewItems to call the above getter
- (void) setNumberOfNewItems:(NSNumber*)number { }

//  This ensures that changes to the children set causes KVO calls to the above getter
+ (NSSet*) keyPathsForValuesAffectingNumberOfNewItems
{
    return [[NSSet alloc] initWithObjects:@"children", nil];
}

这会导致 numberOfNewItems 在表格单元格的objectValue获取新 c> c>

What that does is cause numberOfNewItems to be recalculated any time the table cell's objectValue gets a new child added to or removed from its children to-many relationship.

在childItem类中,有一个地方是childItem从isNew转换为not-new:

In the childItem class, there's this in the one place that the childItem is transitioned from isNew to not-New:

// If collapsing an item, mark it as not new
if (!isExpanded.boolValue && self.isNewValue) {
    self.isNew = @NO;
    [self.parent setNumberOfNewItems:nil]; // Triggers KVO for button binding
}

... 是使用父级的空setNumberOfNewItems setter强制按钮绑定调用getter。因此,每次项目被标记为非新的时,枚举整个多对多的关系。我想应该可以改进,但我还没有玩过。

... and what that does is use the parent's empty setNumberOfNewItems setter to force the button binding to call the getter. So the whole to-many children relationship is enumerated each time an item is marked not-new. I supposed that could be improved, but I haven't played around with that yet.

我利用一个事实,一个项目被标记为不是新的只有一个地方在我的代码。如果您在子项中有几项重置或设置isNew,您可以在childItem类中重写setIsNew以调用 self.parent setNumberOfNewItems:nil

I took advantage of the fact that an item is marked not-new only one place in my code. If you have several things resetting or setting isNew in the child, you might override setIsNew in the childItem class to call self.parent setNumberOfNewItems:nil instead.

这里的诀窍是让父亲将自己添加为所有孩子的isNew关键路径的KVO观察者将是一个可怕的痛苦。所以我想避免。如果你只是让孩子在父级中调用空的setter,父类可以拥有计算,并且在绑定使用之外没有KVO。

The trick here is that having the parent add itself as a KVO observer for the isNew keypath for all children would be a terrible pain. So I wanted to avoid that. If you simply have the children call the empty setter in the parent, the parent can own the calculation, and there's no KVO outside what the bindings use.

看起来像这样:

>

这篇关于iTunes的计数按钮使用Cocoa绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆