【问题标题】:Creating an array item with a property that's dependent on an aggregate function on the array itself使用依赖于数组本身的聚合函数的属性创建数组项
【发布时间】:2014-02-20 21:46:38
【问题描述】:

假设我有一个 NSArrayController 包含许多“blob”对象。这些对象通过绑定显示在基于视图的NSTableView 中。

现在假设每个“blob”对象都包含一个名为amount 的属性。对于表格每一行中的NSViews 之一,我想显示amount / max_amount_in_array

换句话说,我需要同时将我的单元格绑定到NSArrayControllerarrangedObjects.@max.amountNSTableViewCellobjectValue.amount 并执行我的计算。

有没有办法使用绑定很好地处理这个问题?

目前我唯一的想法是在“blob”中拥有一个ratio 属性,并在每次将对象添加到数组时自己重新计算它。这很有可能,但似乎应该有一种更像绑定的方式来解决问题。

【问题讨论】:

    标签: objective-c macos cocoa cocoa-bindings nsarraycontroller


    【解决方案1】:

    我现在已经这样做了:

    [libraryCell.myView bind:@"amount"
                    toObject:libraryCell
                 withKeyPath:@"objectValue.amount"
                     options:nil];
    
    [libraryCell.myView bind:@"max"
                    toObject:_librariesController
                 withKeyPath:@"arrangedObjects.@max.amount"
                     options:nil];
    

    因此,myView 中有两个属性(数量和最大值),当其中任何一个发生变化时,我都会进行计算并相应地更新显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2023-03-08
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多