[Cocoa]_[初级]_[NSTableView之自定义NSImageButtonCell]

来源:互联网 发布:云端软件 知乎 编辑:程序博客网 时间:2024/05/17 09:06

场景:在布局NSTableView的时候,在表格中添加带有多个图片和文字的NSCell,让界面更加符合我们的需求

下面是一个自定义的NSButtonCell

MqjImageButtonCell.h

#import <Cocoa/Cocoa.h>@interface MqjImageButtonCell : NSButtonCell{    NSImage* checkImage[2];    int isChecked;    int imageOffset;}@property (readwrite,assign) int isChecked;@end


MqjImageButtonCell.m

#import "MqjImageButtonCell.h"@implementation MqjImageButtonCell@synthesize isChecked;-(void)awakeFromNib{        NSString* checkPath = [[NSBundle mainBundle] pathForResource:@"check-yes" ofType:@"png"];    checkImage[1] = [[NSImage alloc] initByReferencingFile:checkPath];        NSString* uncheckPath = [[NSBundle mainBundle] pathForResource:@"check-no" ofType:@"png"];    checkImage[0] = [[NSImage alloc] initByReferencingFile:uncheckPath];        isChecked = 0;    imageOffset = 6;    }- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame                       ofView:(NSView *)controlView{    if([event type]!= NSLeftMouseDown)    {        return NSCellHitContentArea;    }        NSTableView *myView = (NSTableView*)controlView;        NSPoint p = [event locationInWindow];    NSPoint local_point = [myView convertPoint:p fromView:nil];        NSUInteger row = [myView rowAtPoint:local_point];        //    NSLog(@"hitTestForEvent %lu",row);    NSSize size = [checkImage[isChecked] size];    NSRect imageRect = cellFrame;    local_point.y = imageRect.origin.y;            CGFloat xr = imageOffset+size.width+2;    imageRect.origin.x = xr;    imageRect.size.width = size.width;    imageRect.size.height = size.height;            //    NSLog(@"local_point.x: %f:%f:%f",local_point.x,xr,cellFrame.origin.x);    //把tableView上的NSTextField清空    NSMutableArray *array =[[NSMutableArray alloc] initWithArray:[myView subviews]];    [array makeObjectsPerformSelector:@selector(removeFromSuperview)];        if (local_point.x<xr)    {        //1.通知复选框有点击操作.        [myView.delegate performSelector:@selector(setCheckItem:)                              withObject:[NSNumber  numberWithInteger:row]];        return NSCellHitContentArea;    }                return NSNullCellType;}- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{    //1.画check    NSImage* image = checkImage[isChecked];    NSRect imageRect = cellFrame;    imageRect.origin.x = imageOffset;    imageRect.origin.y = imageRect.origin.y+(imageRect.size.height - image.size.height)/2;    imageRect.size = image.size;        [image drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver             fraction:1 respectFlipped:YES hints:nil];        //画logoImage    NSRect rect =imageRect;    rect.origin.x +=20;    [[super image] drawInRect:rect fromRect:NSZeroRect operation:NSCompositeSourceOver                     fraction:1.0 respectFlipped:YES hints:nil];            NSSize strSize = [[self title] sizeWithAttributes:nil];    NSRect frameTemp = rect;    frameTemp.origin.y = frameTemp.origin.y+(frameTemp.size.height - strSize.height)/2;    frameTemp.origin.x += rect.size.width + 10;    frameTemp.size = strSize;    NSMutableAttributedString *titleStr =[[NSMutableAttributedString alloc]                                          initWithString:[super title]];        [titleStr addAttribute:NSForegroundColorAttributeName value:[NSColor redColor]                     range:NSMakeRange(0, titleStr.length)];        [titleStr drawInRect:frameTemp];    }@end

显示效果:


0 0
原创粉丝点击