我试图像在Skype的消息窗口中为用户提供的那样,在大纲视图中创建单元格。
为此,我创建了一个自定义类:
IconNameCell.h
@interface IconNameCell : NSTextFieldCell {
//@private
NSImage *userImage; // size (17,17)
NSImage *statusIcon; // size (14,14)
NSString *cellText;
}
@property (readwrite, retain) NSImage *userImage;
@property (readwrite, retain) NSImage *statusIcon;
@property (readwrite, retain) NSString *cellText;
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
@end
IconNameCell.m
@implementation IconNameCell
@synthesize userImage;
@synthesize statusIcon;
@synthesize cellText;
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
@try{
// Inset the cell frame to give everything a little horizontal padding
NSRect anInsetRect = NSInsetRect(cellFrame,2.0,0);
//FIXME: flip coordinates and size can be set in accessor methods
// setting userImage and statusIcon in flipped coordinate
[userImage setFlipped:YES];
[statusIcon setFlipped:YES];
// setting size of image and icon
[userImage setSize:NSMakeSize(25.0, 25.0)];
[statusIcon setSize:NSMakeSize(15.0, 17.0)];
// setting attributes of cell text
NSMutableParagraphStyle *dParagraphStyle = [[NSMutableParagraphStyle alloc] init];
[dParagraphStyle setAlignment:NSLeftTextAlignment];
NSColor *colorOfText;
if ([self isHighlighted]) {
colorOfText = [NSColor whiteColor];
}
else {
colorOfText = [NSColor blackColor];
}
NSMutableDictionary * dTitleAttributes = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
colorOfText,NSForegroundColorAttributeName,
[NSFont systemFontOfSize:11.0],NSFontAttributeName,
dParagraphStyle, NSParagraphStyleAttributeName,
nil];
// getting sizes
NSSize cellTextSize = [cellText sizeWithAttributes:dTitleAttributes];
NSSize userImageSize = [userImage size];
NSSize statusIconSize = [statusIcon size];
// making layout boxes for all elements
// vertical padding between the lines of text
float dVerticalPadding = 2.0;
// horizontal padding between two images
float padBtwnImgs = 4.0;
// horizontal padding between image and text
float padBtwnImgText = 6.0;
NSString *userImageName = [userImage name];
NSLog(@"userImageName - %@ / cellText- %@",userImageName,cellText); // getting null for userImageName
//if ([userImageName isEqualToString:@"current_D.png"]) {
//FIXME: this is juggad and should be corrected
NSRange rangeOfComma = [cellText rangeOfString:@","];
if (rangeOfComma.length !=0 ) {
//<#statements#>
// userImage box: center the userImage vertically inside of the inset rect
NSRect cellTitleBox = NSMakeRect(anInsetRect.origin.x,
anInsetRect.origin.y + anInsetRect.size.height*.5 - cellTextSize.height*.5,
cellTextSize.width,
cellTextSize.height);
// drawing cell text
[cellText drawInRect:cellTitleBox withAttributes:dTitleAttributes];
}
else {
// userImage box: center the userImage vertically inside of the inset rect
NSRect userImageBox = NSMakeRect(anInsetRect.origin.x,
anInsetRect.origin.y + anInsetRect.size.height*.5 - userImageSize.height*.5,
userImageSize.width,
userImageSize.height);
// statusIcon box: center the statusIcon vertically inside of the inset rect
NSRect statusIconBox = NSMakeRect(userImageBox.origin.x + userImageBox.size.width + padBtwnImgs,
anInsetRect.origin.y + anInsetRect.size.height*.5 - statusIconSize.height*.5,
statusIconSize.width,
statusIconSize.height);
// cellTitleBox: vertically aligning text
NSRect cellTitleBox = NSMakeRect(statusIconBox.origin.x + statusIconBox.size.width + padBtwnImgText,
anInsetRect.origin.y + anInsetRect.size.height*.5 - cellTextSize.height*.5,
cellTextSize.width,
cellTextSize.height);
// drawing user image
[userImage drawInRect:userImageBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
// drawing user status
[statusIcon drawInRect:statusIconBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
// drawing cell text
[cellText drawInRect:cellTitleBox withAttributes:dTitleAttributes];
}
}
@catch (NSException *e) {
NSLog(@"IconNameCell -%@",e);
}
}
@end
第二,我为大纲视图分配了文本字段单元格,该类为:IB中的IconNameCell
第三,我在委托中使用此代码在自定义单元格中设置图像,图标和用户名-
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item{
if ([[tableColumn identifier] isEqualToString:@"userInfo"]) {
// some relevant code
if( [[item onlineStatus] intValue] == 0 ){
[cell setStatusIcon:[NSImage imageNamed:@"offline.png"]];
}
else{
[cell setStatusIcon:[NSImage imageNamed:@"online.png"]];
}
//similarly, setting attribute for userImage and cellText
}
我面临着两个问题:1.在大纲视图中选择一行或另一行时,应用程序经常崩溃。早些时候,我没有使用自定义单元格,而是使用了三个不同的列-用户图像,用户状态和用户名,所以效果很好! 2.对于日志:NSLog(@“ userImageName-%@ / cellText-%@”,userImageName,cellText);尽管我应该为其获取一些字符串值,但我正在将它作为用户图像名称(空)。
谁能建议我一些解决方案?
谢谢,
Miraaj
最佳答案
在大纲视图中选择一行或另一行时,应用程序经常崩溃。
请修改您的问题以包含崩溃日志。
2.对于日志:NSLog(@“ userImageName-%@ / cellText-%@”,userImageName,cellText);尽管我应该为其获取一些字符串值,但我正在将它作为用户图像名称(空)。
您是否已使用setName:
设置图像名称或从imageNamed:
获取了图像名称?否则,它没有一个,并且此输出正确。