MacOS开发(OC)----NSTableView类相关属性跟方法

一、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类相关属性跟方法附件》

因CSDN要积分,也可以加QQ群找到资源。本人QQ:872180981

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值