NSTableColumn 使用 @sum 之類的集合運算符綁定 (NSTableColumn binding using Collection Operators like @sum)


問題描述

NSTableColumn 使用 @sum 之類的集合運算符綁定 (NSTableColumn binding using Collection Operators like @sum)

Mac OS X. CoreData app. NSTableView controlled by NSArrayController bound to managed object context for the Country entity. The Country entity has a 'name' attribute and a to‑many relationship, 'branches', to a Branch entity. The Branch entity has a 'sales' attribute (an NSNumber).

The NSTableView has two NSTableColumns. The first shows the name of the Country. The second should show the total sales for that Country across all its Branches.

The first column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'name'. No problem there.

The second column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'branches.@sum.sales'. This doesn't work. I get the error message: " addObserver:forKeyPath:options:context:] is not supported. Key path: @sum.sales"

If, instead, I add a 'totalSales' method to my Country class and the method is implemented as follows:

‑ (NSNumber *)totalSales
{
    return [[self branches] valueForKeyPath:@"@sum.sales"];
}

and I then bind the column to 'totalSales' it works fine. My understanding of the Collection Operators documentation is that this should be the same as simply binding to 'branches.@sum.sales'. I can't see why the latter doesn't work. Any ideas? I have seen similar questions in this and other forums, but have yet to see an explanation or solution.

‑‑‑‑‑

參考解法

方法 1:

I don't know if this is still topic for you, but it surely does need an answer.

The second column's value should be bound to NSArrayController in exactly the sam way as first. I don't know why you made it differently and what actually you wanted to achieve.

Your first task was to bind table columns to array columns and this works the same way for all the columns and types.

Second task is to get sum of certain NSTableColumn bound to certain other object, like NSTextfield. And this is done like this:

    [totalCountField bind: @"value" toObject: arrayController
          withKeyPath:@"arrangedObjects.@sum.price" options:nil];

(by jfewtrmbpro)

參考文件

  1. NSTableColumn binding using Collection Operators like @sum (CC BY‑SA 3.0/4.0)

#cocoa #nstablecolumn #binding #macos






相關問題

為什麼 NSWindow 或 NSView 實例處理它自己的鍵事件,而不是它的委託? (Why does an NSWindow or NSView instance handle its own key events, and not its delegate?)

通過 IKImage 視圖預覽圖像 (Previewg images via IKImage View)

Gaya Jendela Kustom di Kakao (Custom Window Style in Cocoa)

Apakah NSURLConnections termasuk dalam model atau pengontrol saya? (Do NSURLConnections belong in my model or controller?)

Эквівалент ключа NMenuItem не працуе, калі меню схавана (NSMenuItem Key Equivalent not working when menu is hidden)

透明窗口中的圓形 NSView (Rounded NSView in a Transparent Window)

單元格中不可見的 UITextField 已將 userInteractionEnabled 設置為 No (UITextField in cell that is not visible has userInteractionEnabled set to No)

如何為自定義 NSImageRep 子類實現 -draw (How to implement -draw for custom NSImageRep subclass)

從 Objective-C 中的核心數據元素獲取和設置值? (Getting and Setting values from Core Data elements in Objective-C?)

來自 URL 編碼問題的 NSArray (NSArray from URL encoding problem)

停靠欄下的窗口化opengl遊戲中的雙光標 (Double cursor in windowed opengl game under dock)

在特定的 NSTableview 單元格中對角線繪製線條 (Draw Lines Diagonally in a particular NSTableview cell)







留言討論