基于视图的 NSOutlineView 中 NSTextField 的委托事件?

发布于 2024-12-23 14:31:37 字数 683 浏览 1 评论 0原文

我有一个完美运行的基于视图的 NSOutlineView ,并在我的项目中设置了正确的数据源。现在我想允许用户更改某些条目。所以我使 IB 中的 NSTextField 可编辑。对于基于单元格的 NSOutlineView,您可以使用委托方法 outlineView:setObjectValue:forTableColumn:byItem:,但它不适用于基于视图的 NSOutlineView > 如 NSOutlineViewData 协议的头文件中所述:

/* View Based OutlineView:此方法不适用。 */

(void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;

所以我搜索了另一个委托方法并找到了outlineView:shouldEditTableColumn:item:。但是这个委托方法不会被触发。可能是因为我没有编辑单元格。

所以我的问题是:除了为每个 NSTextField 分配一个委托之外,还有其他方法可以注意到行的更改吗?

I have a flawless functioning view-based NSOutlineView with a proper set-up datasource in my project. Now I want to allow the user to change certain entries. So I made the NSTextField in the IB editable. For a cell-based NSOutlineView you can use the delegate method outlineView:setObjectValue:forTableColumn:byItem: however it's not available for a view-based NSOutlineView as stated in the header file for the NSOutlineViewData protocol:

/* View Based OutlineView: This method is not applicable.
*/

(void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;

So I searched for another delegate method and found outlineView:shouldEditTableColumn:item:. However this delegate method doesn't get fired. Probably because I'm not editing a cell.

So my question is: Is there any other way to notice when a row changed than having a delegate for each NSTextField?

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

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

发布评论

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

评论(2

贱贱哒 2024-12-30 14:31:37

您的文本字段需要在 Interface Builder 中可编辑是正确的。

接下来,使您的控制器符合 NSTextFieldDelegate。然后,在outlineView:viewForTableColumn:item: 中设置文本字段的委托,如下所示:

tableCellView.textField.delegate = self

这是一个简化的示例,您已在其中实现了为大纲视图的项目返回表格单元格视图的方法。

-(NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    NSTableCellView *tableCellView = [outlineView makeViewWithIdentifier:@"myTableCellView" owner:self];

    MyItem *myItem = (MyItem *)item; // MyItem is just a pretend custom model object 
    tableCellView.delegate = self;
    tableCellView.textField.stringValue = [myItem title];

    tableCellView.textField.delegate = self;

    return result;
}

然后,控制器应该收到 controlTextDidEndEditing 通知:

- (void)controlTextDidEndEditing:(NSNotification *)obj
{
    NSTextField *textField = [obj object];
    NSString *newTitle = [textField stringValue];

    NSUInteger row = [self.sidebarOutlineView rowForView:textField];

    MyItem *myItem = [self.sidebarOutlineView itemAtRow:row];
    myItem.name = newTitle;  
}

You are correct that your text field needs to be editable in Interface Builder.

Next, make your controller conform to NSTextFieldDelegate. Then, set the delegate for the text field in outlineView:viewForTableColumn:item:, like so:

tableCellView.textField.delegate = self

Here's a simplified example, where you've implemented the method for returning the table cell view for an item for your outline view.

-(NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    NSTableCellView *tableCellView = [outlineView makeViewWithIdentifier:@"myTableCellView" owner:self];

    MyItem *myItem = (MyItem *)item; // MyItem is just a pretend custom model object 
    tableCellView.delegate = self;
    tableCellView.textField.stringValue = [myItem title];

    tableCellView.textField.delegate = self;

    return result;
}

Then, the controller should get a controlTextDidEndEditing notification:

- (void)controlTextDidEndEditing:(NSNotification *)obj
{
    NSTextField *textField = [obj object];
    NSString *newTitle = [textField stringValue];

    NSUInteger row = [self.sidebarOutlineView rowForView:textField];

    MyItem *myItem = [self.sidebarOutlineView itemAtRow:row];
    myItem.name = newTitle;  
}
始于初秋 2024-12-30 14:31:37

好吧,苹果似乎希望我们使用每个 NSTextField 的委托方法,如 此处

此方法旨在与基于单元格的表视图一起使用,不得与基于视图的表视图一起使用。相反,目标/操作用于视图单元中的每个项目。

所以目前没有其他方法可以做到这一点。

Well, it seems like Apple wants us to use the delegate methods of each NSTextField as stated here:

This method is intended for use with cell-based table views, it must not be used with view-based table views. Instead target/action is used for each item in the view cell.

So there's currently no other way to do this.

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