在基于视图的 NSOutlineView 中更改 NSTableCellView 的 objectValue 不会传播到 dataSource

发布于 2024-12-13 19:39:45 字数 1195 浏览 0 评论 0 原文

我使用基于视图的 NSOutlineView 来显示和选择科学应用程序的分层结构项目。

view-based NSOutlineView

大纲列中的每一行代表一个项目,由项目特定的图标表示(在图片)、显示是否选择该项目的复选框以及该项目的名称。我需要图标、复选框和名称出现在同一单元格中,因此我使用基于视图的 NSOutlineView。

我已经实现了 NSOutlineViewDataSource 协议来向大纲视图提供数据。

方法 outlineView:objectValueForTableColumn:byItem: 提供一个自定义对象,该对象具有属性 BOOL selectedNSString *name

我在 IB 中的自定义表格单元格视图的组成如下:

IB 中基于视图的 NSOutlineView

我将复选框值绑定到 < code>objectValue.selected 并将标签值设置为 objectValue.name

正如我所希望的,大纲视图很好地显示了 objectValue 提供的名称和选择状态。

但是,如果我更改复选框的状态,则在我的数据源中不会触发 NSOutlineViewDataSource 协议中定义的方法 outlineView:setObjectValue:forTableColumn:byItem: 来提供新更改的对象值。请注意,如果我不使用单元格的自定义视图,则此操作有效。

我通过将 NSLog 语句插入到作为 setSelected 方法中,检查了单击复选框时表格单元格视图的 objectValue.selected 是否实际发生更改>对象值。 selected 成员正确更改状态。

如何将 objectValue 的更改传播回我的数据源模型?我已经检查了 NSOutlineView 的委托方法,但找不到一种方法来表明单元格视图的 objectValue 已被我的复选框更改(即单元格视图已“结束编辑”)。我还缺少其他一些基本点吗?

I use a view-based NSOutlineView to display and select hierarchically structured items for a scientific application.

view-based NSOutlineView

Each row in the outline column represents an item, signified by an item-specific icon (all the same in the picture), a checkbox that shows if the item is selected, and the name of the item. I need the icon, the checkbox and the name to appear in the same cell, hence I am using a view-based NSOutlineView.

I have implemented the NSOutlineViewDataSource protocol to supply data to the the outline view.

The method outlineView:objectValueForTableColumn:byItem: supplies a custom object that has the properties BOOL selected and NSString *name.

My custom table cell view in IB is composed as follows:

view-based NSOutlineView in IB

I bound the check box value to objectValue.selected and the label value to objectValue.name.

As I hoped, the outline view displays nicely the name and selection state supplied by the objectValue.

However, if I change the state of the check box, the method outlineView:setObjectValue:forTableColumn:byItem: that is defined in the NSOutlineViewDataSource protocol is not triggered in my dataSource to supply the newly changed object value. Note that if I don't use a custom view for the cell this works.

I checked whether the table cell view's objectValue.selected actually gets changed when clicking the check box by inserting an NSLog statement into the setSelected method of the object that is passed as objectValue. The selected member changes state correctly.

How do I propagate the change of the objectValue back to my dataSource's model? I have checked the NSOutlineView's delegate methods, but can't find a way to signal that the cell view's objectValue was changed by my check box (i.e., that the cell view has "ended editing"). Is there some other fundamental point I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2024-12-20 19:39:45

setObjectValue 不适用于基于视图的视图:

来自 header::

/* View Based OutlineView: This method is not applicable.
 */
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;

setObjectValue doesnt work for view based ones:

from header::

/* View Based OutlineView: This method is not applicable.
 */
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;
笨笨の傻瓜 2024-12-20 19:39:45

我能够通过创建 NSTableCellView 的子类,使其成为所包含的 NXTextField 的委托,并使用编辑后的值来更新 NSTableCellView 来解决此问题code> 的对象值。

class CategoryNameTableViewCell : NSTableCellView, NSTextFieldDelegate {

    func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
        guard var category = self.objectValue as! Category? else {
            Swift.print("Tried to edit category cell with no object!")
            return false
        }
        category.name = control.stringValue
        category.saveChanges()
        return true
    }

}

I was able to solve this problem by creating a subclass of NSTableCellView, making it the delegate of the contained NXTextField, and using the edited value to update NSTableCellView's object value.

class CategoryNameTableViewCell : NSTableCellView, NSTextFieldDelegate {

    func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) -> Bool {
        guard var category = self.objectValue as! Category? else {
            Swift.print("Tried to edit category cell with no object!")
            return false
        }
        category.name = control.stringValue
        category.saveChanges()
        return true
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文