[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
- [Cocoa]_[初级]_[NSTableView之自定义NSImageButtonCell]
- [Cocoa]_[初级]_[NSTableView添加背景图片]
- [Cocoa]_[初级]_[自定义NSTableHeaderView]
- [Cocoa]_[NSTableView]_[基本使用]
- [Cocoa]_[NSTableView]_[添加复选框]
- [Cocoa]_[初级]_[NSTableView--数据操作和表格操作要注意的问题]
- [Cocoa]_[初级]_[自定义的界面上绘制图片]
- [Cocoa]_[初级]_[NSThread的使用]
- [Cocoa]_[初级]_[如何复制NSView]
- [Cocoa]_[初级]_[文件拖放操作]
- [Cocoa]_[初级]_[Cocoa设计模式总结]
- [Cocoa]_[初级]_[Cocoa入门级界面开发之界面初级程序编辑的步骤和实现方法]
- [Cocoa]_[初级]_[在自定义View中单击鼠标右键弹出菜单栏]
- [Cocoa]_[初级]_[NSButton之改变title标题的颜色]
- [Cocoa]_[初级]_[用NSImage把大图制作成小图]
- [Cocoa]_[初级]_[vcf文件的生成和导出]
- [Cocoa]_[初级]_[NSPopUpButton重绘实例]
- [Cocoa]_[初级]_[使用NSXMLElement 创建一个XML文件]
- JQPAGINATION分页插件实践
- monoslam,scenelib,spcaview
- quick Cocos2d-x lua 八方向摇杆代码实现
- 数据结构之---C语言实现图的十字链表存储表示
- Snail—UI学习之弹出按钮选择项UIActionSheet
- [Cocoa]_[初级]_[NSTableView之自定义NSImageButtonCell]
- JavaScript replaceall 方法
- 聊天工具条
- hdoj1213 How many tables?(并查集)
- Java(07—29)学习总结
- 【交换安全】DAI - Dynamic ARP Inspection 详解
- source sh执行脚本的区别
- 2120 Ice_cream's world I
- 关于Java的常见误解