2010-08-02 48 views

回答

1

有一個很好的例子是如何做到這一點:http://www.cocoadev.com/index.pl?IconAndTextInTableCell你讓你自己定製的NSCell,無論繪製圖片和文字

@interface IconCell : NSCell 
{ 
NSArray * cellValue; 
} 
- (void)setObjectValue:(id <NSCopying>)object; 
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView; 

@implementation IconCell 

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
{ 
NSDictionary * textAttributes = 
    [NSDictionary dictionaryWithObjectsAndKeys:[NSFont 
userFontOfSize:10.0],NSFontAttributeName, nil]; 
NSPoint cellPoint = cellFrame.origin; 

[controlView lockFocus]; 

[[cellValue objectAtIndex:1] compositeToPoint:NSMakePoint(cellPoint.x+2, 
cellPoint.y+14) operation:NSCompositeSourceOver]; 
[[cellValue objectAtIndex:0] drawAtPoint:NSMakePoint(cellPoint.x+18, 
cellPoint.y) withAttributes:textAttributes]; 

[controlView unlockFocus]; 
} 

- (void)setObjectValue:(id <NSCopying>)object 
{ 
    cellValue = (NSArray *)object; 
} 
@end 
+0

值得指出的是該單元需要的數據源就可以提供一個數組(文件名,圖標)作爲對象值。提問者的額外榮譽:調整單元格代碼以期望NSURL作爲對象值,並從中獲取文件名和圖標。 – 2010-08-02 08:49:19

+0

酷,好東西。我現在正在做的是通過數組控制器將我的表視圖綁定到捆綁ID數組,通過值轉換器將數據包ID轉換爲應用程序顯示名稱。我想我的價值轉換器可以放出圖像和文本組合爲單元繪製。無論如何,我需要現在閱讀關於NSCell。我之前沒有處理過的東西... – 2010-08-02 09:58:28

+2

使用-tableView:willDisplayCell:設置圖像部分的委託方法避免了需要使用數組作爲對象值。它也可以與Bindings自由混合。 – 2010-08-02 10:09:11

相關問題