使用绑定将图像插入 NSTableView

发布于 2024-08-24 04:08:08 字数 201 浏览 5 评论 0原文

我有一个 NSTableView 绑定到一个具有两列的 NSArrayController 。一列绑定到阵列控制器的排列对象并显示一个字符串。

我想在另一列中显示图像,但我无法使其工作。我已将 NSImageCell 拖到列中并手动设置图像,但它不会在运行时显示。我已经仔细检查过,图像位于我的资源目录中。

我错过了什么吗?我还应该做什么才能使该图像出现?

I've an NSTableView bound to an NSArrayController with two columns. One column is bound to the arranged objects of the array controller and displays a string.

I'd like to display an image in the other column, but I just can't make it work. I've dragged an NSImageCell to the column and set the image by hand but it won't show up at runtime. I've double checked and the image is in my resources directory.

Am I missing something? What else should I do to make that image appear?

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

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

发布评论

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

评论(1

倾`听者〃 2024-08-31 04:08:08

那么您想让每一行显示相同的图像吗?这就是你“手动”设置它的原因吗?为此,您可以将 NSTableViewDataSource 方法与绑定混合使用。这个想法是你的字符串列将像往常一样被绑定,但你的图像列没有绑定。它有自己的标识符集(如“imageColumn”)。然后,您使用 numberOfRowsInTableView: 和 tableView:objectValueForTableColumn:row: 提供数组控制器的对象计数(因此它具有正确的行数),并且在请求右列的值时始终返回静态图像(检查 id对于你的“imageColumn”),否则返回 nil。

如果图像不是静态的(也就是说,您想将其用作某种指示符),您可以使用上述方法(基于某些值返回某些图像)或绑定。要使用绑定,您可以向数组控制器所持有的任何类添加一个属性,例如“状态”(数字)。然后,您将使用自定义 NSValueTransformer 将状态编号转换为相应的图像。您的列将使用值转换器绑定到数组控制器的arrangedObjects.status(有关使用说明,请参阅 NSValueTransformer - 您必须注册它,然后使用它的名称)。结果是您的列中对应于特定状态的图像。

So you want to have the same image appear for each row? Is that why you're setting it "by hand"? For that, you can mix NSTableViewDataSource methods with bindings. The idea is your string column will be bound as usual, but your image column isn't bound. It has its identifier set (like "imageColumn"). You then use numberOfRowsInTableView: and tableView:objectValueForTableColumn:row: to provide the array controller's object count (so it has the right number of rows) and simply always return your static image when it asks for the value for the right column (checking the id for your "imageColumn"), returning nil otherwise.

If the image is not static (that is, you want to use it as an indicator of some kind) you can use the above method (return some image based some value) OR bindings. To use Bindings, you might add a property to whatever class your array controller is holding, like "status" (a number). You'll then use a custom NSValueTransformer that transforms the status number into a corresponding image. Your column will be bound to the array controller's arrangedObjects.status, using the value transformer (see NSValueTransformer for instructions for use - you have to register it, then use its name). The result is an image in your column that corresponds to a certain status.

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