NSTableView 在一个单元格中显示图像
我可能会问一个非常简单的问题,但我不知道如何摆脱这个问题, 要求是,
- 表格应显示图像和旁边的一些文本。为此,我在表中添加了两列
- NSTableView 应该是透明的。为此,我创建了一个 CustomNSTableView 类,在内部重写 drawRect 方法以不绘制背景,并告诉单元格不要绘制背景。
- 现在下一个障碍是如何显示图像,我必须覆盖/处理以下方法
。
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSImage *pCellImage = // Get a valid Image ;
if(tableColumn == nil ) return nil;
NSString *colName = [tableColumn identifier];
if([colName isEqualToString:@"firstColumn"]){
NSCell *pCell = [[NSCell alloc]initImageCell:pCellImage];
return pCell;
}else{
NSCell *pCell = [[NSCell alloc]initTextCell:@"Some text"];
return pCell;
}
}
我添加了 tableColumn == nil 条件,如在文档中给出的,每行第一次,当我需要为整行设置单个列时,将使用 tableCOlumn nil 调用此条件,但如果我使用注释 BAD_ACCESS 返回其崩溃,
现在我的问题是如何处理零条件, 另外,在这个函数中,为两个列设置 NSCell 后,它会转到另一个委托函数,
- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
我相信,在正确设置 dataSet 后,我不需要使用这个函数,
通过以下实现,它会崩溃,但我无法弄清楚,出了什么问题,
我正在设置正确的数据源和委托,
I might be asking very simple question, but i am not able to get how to get rid of this,
the requirement is,
- Table should display image and some text next to that. To do that, i have added two columns in the table
- NSTableView should be transparent. To do that, i have made a CustomNSTableView class, inside override drawRect method for not to draw background, and tell cell not to drawBackground.
- Now the next hurdle is how to display an image, i had to override/handled following method
.
- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSImage *pCellImage = // Get a valid Image ;
if(tableColumn == nil ) return nil;
NSString *colName = [tableColumn identifier];
if([colName isEqualToString:@"firstColumn"]){
NSCell *pCell = [[NSCell alloc]initImageCell:pCellImage];
return pCell;
}else{
NSCell *pCell = [[NSCell alloc]initTextCell:@"Some text"];
return pCell;
}
}
i added tableColumn == nil condition, as in the documentation its given, first time for each row , this will be called with tableCOlumn nil when i need to set a single column for entire row, but if i return its crashing with the comment BAD_ACCESS,
Now my question is how to handle nil condition,
Also in this function after setting NSCell for both Column it goes to another delegate function
- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
I believe, after setting dataSet properly i don't need to use this function,
With the following implementation its crashing, but i am not able to figure it out, what is wrong ,
i am setting proper dataSource and delegate,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过调用 setBackgroundColor 并传递清晰的颜色?
如果你使用这个方法,记得告诉周围的 NSScrollView 不要绘制它的背景。
或者,考虑将表格背景提供为 CALayer。
关于在单列中绘制图像和文本,Apple 的开发人员关系发布了一个自定义 NSCell,ImageAndTextCell.您可以将此单元格与 NSTableView 和 NSOutlineView 实例一起使用。此自定义单元格用于在单列中绘制图像和文本。
Have you tried calling setBackgroundColor and passing the clear colour?
If you use this method, remember to tell the surrounding NSScrollView not to draw it's background.
Alternatively, consider providing the table's background as a CALayer.
With regard to drawing the image and text in a single column, Apple's developer relations have published a custom NSCell, ImageAndTextCell. You can use this cell with NSTableView and NSOutlineView instances. This custom cell deals with drawing a image and text within a single column.