一、NSTableView的组成元素
二、NSTableView跟NSTabViewDelegate和NSTableViewDataSource的关系
三、实现自定义的Cell、带有滑动效果、点击功能的NSTableView。
/*-----------------------------------------------------------------------
初始化
*/
NSNib* myNib = [[NSNib alloc] initWithNibNamed:@"MYCellView" bundle:nil];
[_tableView registerNib:myNib forIdentifier:@"myCellView"];
//设置行高
[_tableView setRowHeight:80];
//在storyboard中设置horizontal grid 和 vertical grie来显示水平和垂直分割线。
/*-----------------------------------------------------------------------
实现代理跟数据源相关方法NSTabViewDelegate,NSTableViewDataSource
*/
//数据源方法(返回NSTableView有多少行)
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return 20;
}
//返回每行的view.就是我们之前注册的。
- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
{
MYCellView* cellView = [tableView makeViewWithIdentifier:@"myCellView" owner:nil];
//设置图片
//cellView.pictureView.image = [NSImage imageNamed:@"qqqq"];
//设置文字
cellView.nameLable.stringValue = @"aaaabbbb";
return cellView;
}
//选中的响应
-(void)tableViewSelectionDidChange:(nonnull NSNotification *)notification{
NSTableView* tableView = notification.object;
//选中的行数
//tableView.selectedRow;
//选中的列数(列数无效,因为只能同时选中行。不能单独选中Cell)
//tableView.selectedColumn;
NSLog(@"%ld %ld", (long)tableView.selectedRow , (long)tableView.selectedColumn);
}
//手势滑动(要用触摸板,用普通鼠标不能实现)
- (NSArray<NSTableViewRowAction *> *)tableView:(NSTableView *)tableView rowActionsForRow:(NSInteger)row edge:(NSTableRowActionEdge)edge
{
//向左边水滑动
if(edge == NSTableRowActionEdgeTrailing)
{
NSTableViewRowAction* action = [NSTableViewRowAction rowActionWithStyle:NSTableViewRowActionStyleDestructive title:@"DEMO" handler:
^(NSTableViewRowAction *action, NSInteger row){
printf("点击了DEMO");
}];
action.backgroundColor = NSColor.orangeColor;
NSTableViewRowAction* action2 = [NSTableViewRowAction rowActionWithStyle:NSTableViewRowActionStyleDestructive title:@"DEMO1" handler:^(NSTableViewRowAction *action, NSInteger row){
printf("点击了DEMO1");
}];
action2.backgroundColor = NSColor.redColor;
return @[action , action2];
}
if(edge == NSTableRowActionEdgeLeading)
{
NSTableViewRowAction* action = [NSTableViewRowAction rowActionWithStyle:NSTableViewRowActionStyleDestructive title:@"AAA" handler:
^(NSTableViewRowAction *action, NSInteger row){
printf("点击了AAA");
}];
action.backgroundColor = NSColor.orangeColor;
NSTableViewRowAction* action2 = [NSTableViewRowAction rowActionWithStyle:NSTableViewRowActionStyleDestructive title:@"BBB" handler:^(NSTableViewRowAction *action, NSInteger row){
printf("点击了BBB");
}];
action2.backgroundColor = NSColor.redColor;
return @[action , action2];
}
return @[];
}
显示效果:
资源下载:在博客下载区可以找到 《MacOS开发(OC)----NSTableView类相关属性跟方法附件》
因优快云要积分,也可以加QQ群找到资源。本人QQ:872180981