无法创建 NSImage?
每当我这样做时:
xxx = [NSImage imageNamed:@"Package.png"];
xxx 加载,但它的宽度和高度保持为 0。每当我尝试将其加载到 NSImageCell 中时,我都会收到此错误:
NSImageCell 的对象值必须是 NSImage。
有人可以帮我吗?我以前从未遇到过这个问题。
编辑:抱歉,我错过了这一点。因此,当我在数据源委托中执行此操作时,它不起作用,并且在“return cell;'。
NSImageCell* cell = [[NSImageCell alloc] init];
[cell setObjectValue:xxx]; // Using imageNamed doesn't help either
编辑2:这变得越来越严重。我不明白发生了什么,但图像正确加载了高度和宽度,但当我将其添加到 NSImageCell 时它仍然抱怨。
Whenever I do:
xxx = [NSImage imageNamed:@"Package.png"];
xxx loads but it's width and height remain 0. And whenever I try loading it into an NSImageCell I get this error:
NSImageCell's object value must be an NSImage.
Can someone help me out? I've never had this problem before.
Edit: Sorry, I've missed this bit out. So when I do it in the data source delegate it does not work and it shows the above error after 'return cell;'.
NSImageCell* cell = [[NSImageCell alloc] init];
[cell setObjectValue:xxx]; // Using imageNamed doesn't help either
Edit 2: This is becoming aggravating. I don't understand what happened but the image loads height and width properly, but it still complains when I add it to an NSImageCell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我明白了,所以表视图方面毕竟是相关的。
作为数据源,您的工作是返回(并在用户编辑的情况下接收)列中单元格的对象值。
但你并没有返回这样的值;你正在归还一个手机。因此,您尝试将图像单元格(由数据源创建)设置为图像单元格(该列拥有的现有单元格)的值。
日志消息表明您在创建列时已将列的单元格设置为图像单元格,因此您现在需要做的就是更改数据源以始终返回列的对象值,而不是单元格。对于图像列,返回图像。对于文本列,返回字符串。
请注意,NSTableView 的工作方式与 UITableView 不同,其中 UITableViewCells 是 UIView,并且屏幕上的单元格数量与行数一样多;在 NSTableView 中,每个 NSTableColumn 都获取一个且仅有一个数据单元格,并且该单元格用于绘制每一行的该列。单元格绘制其对象值,您可以通过从数据源方法返回该对象值(对象值)来将其提供给单元格。
有关控件(NSTableView 是 NSControl 的一种)及其单元格的文档是 控制和单元编程指南。
I see—so the table view aspect is relevant after all.
As the data source, your job is to return (and receive, in the case of user editing) the object values for the cells in the columns.
But you're not returning such a value; you're returning a cell. Thus, you're trying to set an image cell (created by the data source) as the value of an image cell (the existing one owned by the column).
The log message suggests that you have already set the column's cell as an image cell when you created the column, so all you need to do now is change your data source to always return the object value for the column, not a cell. For an image column, return the image. For a text column, return the string.
Note that NSTableView does not work like UITableView, where UITableViewCells are UIViews and you have as many cells as rows on the screen; in NSTableView, each NSTableColumn gets one and only one data cell, and that one cell is used to draw that column of every row. The cell draws its object value, which you provide to the cell, which you do by returning it (the object value) from your data source method.
The documentation about controls (an NSTableView is a kind of NSControl) and their cells is the Control and Cell Programming Guide.
我的猜测是它返回
nil
,在这种情况下获取宽度/高度将返回 0。尝试:
并确保图像实际上被复制到应用程序的资源文件夹中!
My guess is it's returning
nil
, in which case getting the width/height will return 0.Try:
and make sure the image is actually being copied into your application's Resources folder!
确定您实际上正在接收宽度和高度为零的 NSImage 实例(不是
nil
),下一步是确定发生这种情况的原因。representations
数组的输出是什么? (这应该包含在图像自己的描述中,但我明确包含它,以防万一。)Having established that you are, in fact, receiving an NSImage instance (not
nil
) whose width and height are zero, the next step is to determine why that happens.representations
array? (This should be included in the image's own description, but I include it explicitly in case not.)