使用 NSCell 选择文本(内容)而不是单元格

发布于 2024-10-14 13:55:35 字数 228 浏览 10 评论 0原文

我目前正在一个带有 NSOutlineView 的项目中工作...

当然,我使用 NSCell,并且我需要能够选择单元格内的文本... 或者至少...阻止选择(并突出显示)单元格...

我搜索了 IB 上的所有选项,但找不到正确的选项...

是否有一种方法(无论是否以编程方式)阻止选择/突出显示单元格,也不让用户选择单元格内容?

谢谢 =)

I'm currently working in a project with a NSOutlineView...

I use, of course, NSCell(s) and I need to let the ability to select text inside the cell...
Or at least... prevent the selection (and highlight) of the cells...

I search all options on IB, but can't found the right one...

Is there a way, programmatically or not, to prevent selection/highlighting of cell, nor let user select cell content ?

Thanks =)

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

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

发布评论

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

评论(3

沙沙粒小 2024-10-21 13:55:35

这与 NSCell 没有太大关系,也许您希望在委托中实现 outlineView:shouldSelectItem:

在 NSCell 上,setEnabled:NO 也可能有帮助。来自文档:

setEnabled:(BOOL)flag

禁用单元格的文本更改为灰色。如果单元格被禁用,则它无法突出显示,不支持鼠标跟踪(因此无法参与目标/操作功能),并且无法编辑。但是,您仍然可以通过编程方式更改禁用单元格的许多属性。 (例如,setState: 方法仍然有效。)

That's not much NSCell related, maybe you're looking to implementing outlineView:shouldSelectItem: in your delegate.

On the NSCell, setEnabled:NO, may help too. From the documentation:

setEnabled:(BOOL)flag

The text of disabled cells is changed to gray. If a cell is disabled, it cannot be highlighted, does not support mouse tracking (and thus cannot participate in target/action functionality), and cannot be edited. However, you can still alter many attributes of a disabled cell programmatically. (The setState: method, for instance, still works.)

终陌 2024-10-21 13:55:35

尝试设置:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

您也可以尝试覆盖highlightSelectionInClipRect:,但我不完全确定这会起作用。

Try setting:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

You could also try overriding highlightSelectionInClipRect:, but I'm not totally sure this will work.

箜明 2024-10-21 13:55:35

让我们举一个简单的例子,如下面的大纲视图。有 3 列:firstNamelastNamefullName

在此处输入图像描述

在此特定示例中,假设我们只想允许 firstName 和 < code>lastName 可编辑,而 fullName(可能源自 firstNamelastName)则不可编辑。您可以通过选中或取消选中表列的可编辑复选框来在 Interface Builder 中进行设置。为此,请在表列之一(不是标题,而是大纲视图内部)上单击 3 次;首先选择 NSScrollView,然后选择 NSOutlineView,然后选择 NSTableColumn
在此处输入图像描述

您可以设置如下属性:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

首先为整个列设置默认的可编辑值。如果您需要更多地控制特定行的项目值是否可编辑,可以使用outlineView:shouldEditTableColumn:item:委托方法:

#pragma mark -
#pragma mark <NSOutlineViewDelegate>

- (BOOL)outlineView:(NSOutlineView *)anOutlineView
    shouldEditTableColumn:(NSTableColumn *)tableColumn
               item:(id)item {

    if ([[tableColumn identifier] isEqualToString:@"firstName"] ||
        [[tableColumn identifier] isEqualToString:@"lastName"]) {

        return YES;

    } else if ([[tableColumn identifier] isEqualToString:@"fullName"]) {

        return NO;
    }
    return YES;
}

如果您想控制大纲中的特定行是否可编辑视图是可选的(例如,您可以阻止选择组项目),您可以使用 outlineView:shouldSelectItem:

 - (BOOL)outlineView:(NSOutlineView *)anOutlineView shouldSelectItem:(id)item {
    // if self knows whether it should be selected
    // call its fictional isItemSelectable:method:

    if ([self isItemSelectable:item]) {
        return YES;
    }

    /* if the item itself knows know whether it should be selectable
     call the item's fictional isSelectable method. Here we
     are assuming that all items are of a fictional
      MDModelItem class and we cast `item` to (MDModelItem *)
      to prevent compiler warning */

    if ([(MDModelItem *)item isSelectable]) {
        return YES;
    }

    return NO;
}

Let's take a quick example like the outline view below. There are 3 columns: firstName, lastName, and fullName.

enter image description here

In this particular example, let's say we want to only allow firstName and lastName to be editable while fullName (which is potentially derived from firstName and lastName) is not. You could set this up in Interface Builder by checking or unchecking the editable checkbox for the table column. To do that, click 3 times on one of the table columns (not the header, but inside the outline view); this first selects the NSScrollView, then the NSOutlineView, then an NSTableColumn:
enter image description here

You'd set the attributes like the following:

enter image description here

enter image description here

enter image description here

That provides a start by setting a default editable value for the entire column. If you need more control over whether a particular row's item value should be editable or not, you can use the outlineView:shouldEditTableColumn:item: delegate method:

#pragma mark -
#pragma mark <NSOutlineViewDelegate>

- (BOOL)outlineView:(NSOutlineView *)anOutlineView
    shouldEditTableColumn:(NSTableColumn *)tableColumn
               item:(id)item {

    if ([[tableColumn identifier] isEqualToString:@"firstName"] ||
        [[tableColumn identifier] isEqualToString:@"lastName"]) {

        return YES;

    } else if ([[tableColumn identifier] isEqualToString:@"fullName"]) {

        return NO;
    }
    return YES;
}

If you want to control whether a particular row in the outline view is selectable (for example, you could prevent selection of a group item), you can use outlineView:shouldSelectItem:.

 - (BOOL)outlineView:(NSOutlineView *)anOutlineView shouldSelectItem:(id)item {
    // if self knows whether it should be selected
    // call its fictional isItemSelectable:method:

    if ([self isItemSelectable:item]) {
        return YES;
    }

    /* if the item itself knows know whether it should be selectable
     call the item's fictional isSelectable method. Here we
     are assuming that all items are of a fictional
      MDModelItem class and we cast `item` to (MDModelItem *)
      to prevent compiler warning */

    if ([(MDModelItem *)item isSelectable]) {
        return YES;
    }

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