阅读 237

macOS开发之NSTableView(纯代码)

我们今天来学习NSTableView这个控件,在iOS中UITableView是我们经常使用的,非常了解,可是在macOS中它的使用和功能跟我们iOS中的很不一样,本来想前段时间就想写这篇文章的,可是刚开始使用了解不多,现在使用了好几次了,今天总结一下

在macOS开发中NSTableView本身是不会滚动的,需要嵌套一个NSScrollView才能跟iOS中一样上下滚动,我们今天就学习这个版本,不嵌套的使用也一样就是没有套用的环节

一,初始化NSScrollView容器视图
- (NSScrollView *)scrollView//容器视图
{
    if (!_scrollView) {
        _scrollView = [[NSScrollView alloc] init];
        [_scrollView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
        _scrollView.documentView = self.tableView;
        _scrollView.hasVerticalScroller = YES;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

注解:

  • documentView上面代码中的这个属性是内容视图,直接把NSTableView赋值就行
  • hasVerticalScroller这个属性是BOOL值,是否显示滚动条,默认是否定的不显示
二,初始化NSTableView内容视图
- (NSTableView *)tableView  //考试列表
{
    if (!_tableView) {
        _tableView = [[NSTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.allowsColumnReordering = NO;
        _tableView.allowsColumnResizing = NO;
        _tableView.focusRingType = NSFocusRingTypeNone;
        _tableView.rowHeight = 75;
        _tableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;
        NSTableColumn *Column0 = [[NSTableColumn alloc] initWithIdentifier:@"examColumn0"];
        [_tableView addTableColumn: Column0];
        _tableView.headerView = nil;
    }
    return _tableView;
}

注解:

  • allowsColumnReorderingBOOL值,是否允许拖动分组重新排序,默认是允许的,我们这里就一个分组所以就关闭了
  • allowsColumnResizingBOOL值,是否允许拖动修改分组宽度,默认是允许的,
  • focusRingType的值是枚举,这个属性是它的父类NSView的,NSTableView成为第一响应者之后的外部边框样式
  • selectionHighlightStyle的值也是枚举,cell选中之后的样式,我们自定义不需要这个
  • NSTableColumn这个分组默认是零,所以一定要先添加,要不然不显示cell
  • headerView头部视图默认会显示的,我们不需要所以置空了
  • 纯代码写的cell不需要注册,也没有注册的方法,判断就可以,如果是XIB实现的可以注册,有相应的注册方法,这个也有可能是官方推荐我们使用XIB实现吧
三,实现NSTableViewDelegate回调方法
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return self.dataSource.count;
}
- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    SHExamCellView *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
    if (!cell) {
        cell = [[SHExamCellView alloc] init];
    }
    cell.model = _dataSource[row];
    return cell;
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification{
    NSTableView *tableView = notification.object;
    SHExamModel *model = _dataSource[tableView.selectedRow];
    NSLog(@"---点击了%ld-------%@",tableView.selectedRow,model.planName);
}

这里简单实现了我们常用的三个代理方法,代理方法比较多,其它的方法小伙伴们可以按需实现

  • 点击回调方法点击同一个cell只调用一次,不再重复调用
四,自定义cell
  1. SHExamCellView.h
#import <Cocoa/Cocoa.h>
@class SHExamModel;

@interface SHExamCellView : NSTableCellView
@property (nonatomic , strong) SHExamModel *model;
@end
  1. SHExamCellView.m
#import "SHExamCellView.h"
#import "SHExamModel.h"
@interface SHExamCellView ()
@property (nonatomic , strong) NSView       *line_top;//分割线-上
@property (nonatomic , strong) NSView       *line_left;//分割线-左
@end

@implementation SHExamCellView

- (instancetype)initWithFrame:(NSRect)frameRect
{
    if (self = [super initWithFrame:frameRect]) {
        self.wantsLayer = YES;
        self.layer.backgroundColor = NSColor.whiteColor.CGColor;
        //分割线-上
        [self.line_top mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.height.offset(1);
        }];
        //分割线-左
        [self.line_left mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.equalTo(self);
            make.width.offset(1);
        }];
    }
    return self;
}
- (void)setModel:(SHExamModel *)model
{
    _model = model;
    self.titleLabel.stringValue = model.planName;
}
#pragma mark - lazy
//分割线-上
- (NSView *)line_top
{
    if (!_line_top) {
        _line_top = [[NSView alloc] init];
        _line_top.wantsLayer = YES;
        _line_top.layer.backgroundColor = ColorFromRGB(245, 245, 245, 1.0).CGColor;
        [self addSubview:_line_top];
    }
    return _line_top;
}
//分割线-左
- (NSView *)line_left
{
    if (!_line_left) {
        _line_left = [[NSView alloc] init];
        _line_left.wantsLayer = YES;
        _line_left.layer.backgroundColor = ColorFromRGB(245, 245, 245, 1.0).CGColor;
        [self addSubview:_line_left];
    }
    return _line_left;
}
@end

注解:

  • 这里使用Masonry进行约束,上面的代码是示例,完整度不够,仅供大家参考
    收工

作者:chasitu

原文链接:https://www.jianshu.com/p/cde8b0b62e0f

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐