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

场景:在布局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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值