美文网首页MacOS 开发收录Mac开发
MacOS 开发(六):NSTableView纯代码实现

MacOS 开发(六):NSTableView纯代码实现

作者: SoaringHeart | 来源:发表于2020-04-02 14:18 被阅读0次

    因为系统机制导致,纯代码实现滚动视图的时候必须自己实现 scrollView,随封装如下:


    3201585809938_.pic.jpg
    🌰🌰:
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self.view addSubview:self.tableView.enclosingScrollView];
    }
    
    - (void)viewDidLayout{
        [super viewDidLayout];
    
        [self.tableView.enclosingScrollView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.equalTo(self.view);
            make.width.equalTo(self.view).multipliedBy(0.5);
            make.bottom.equalTo(self.view).offset(0);
        }];
      //备注:enclosingScrollView 和 scrollView 是同一对象
    }
    
    
    -(NNTableView *)tableView{
        if (!_tableView) {
            _tableView = ({
                NNTableView *view = [NNTableView create:CGRectZero];
                view.headerView = nil;
                view.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;//行高亮的风格
    //            view.columnAutoresizingStyle = NSTableViewUniformColumnAutoresizingStyle;
    
                [view addTableColumnWithTitles:@[@"colume0",]];
                if ([self conformsToProtocol:@protocol(NSTableViewDataSource)]) view.dataSource = self;
                if ([self conformsToProtocol:@protocol(NSTableViewDelegate)]) view.delegate = self;
                view.enclosingScrollView.hasHorizontalScroller = false;
                view.enclosingScrollView.hasVerticalScroller = false;
                view.enclosingScrollView.autohidesScrollers = true;//自动隐藏滚动条(滚动的时候出现)
                
                view;
            });
        }
        return _tableView;
    }
    

    视图封装:

    class NNTableView: NSTableView {
        
        lazy var scrollView: NSScrollView = {
            let scrollView = NSScrollView()
            scrollView.backgroundColor = NSColor.red
            scrollView.drawsBackground = false
            scrollView.hasHorizontalScroller = true
            scrollView.hasVerticalScroller = true
            scrollView.autohidesScrollers = true
            
            return scrollView
        }()
        
        var adjustsTableColumnsWidth = true
        
        // MARK: -lifecycle    
        override init(frame frameRect: NSRect) {
            super.init(frame: frameRect)
            
            setupUI()
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
    
            // Drawing code here.
        }
        
        override func layout() {
            super.layout()
            
            if adjustsTableColumnsWidth == true {
                for column in tableColumns {
                    column.width = scrollView.bounds.width/CGFloat(tableColumns.count);
                }
            }
        }
        
        // MARK: -funtions
        func setupUI() {
            scrollView.documentView = self
            
        }
        
    }
    
    @objc public extension NSTableView {
    
        static func create(_ rect: CGRect) -> Self {
            let view: NSTableView = self.init(frame: rect);
            view.autoresizingMask = [.width, .height];
    //        view.columnAutoresizingStyle = .uniformColumnAutoresizingStyle
    
            view.gridStyleMask = .solidVerticalGridLineMask
    //        view.focusRingType = .none //tableview获得焦点时的风格
            view.selectionHighlightStyle = .regular //行高亮的风格
            view.layer?.backgroundColor = NSColor.background.cgColor
            view.usesAlternatingRowBackgroundColors = false //背景颜色的交替,一行白色,一行灰色。设置后,原来设置的 backgroundColor 就无效了。
    //        view.gridColor = NSColor.red
            
            view.appearance = NSAppearance(named: .aqua)
    //        view.headerView = nil;
    
            view.rowHeight = 70;
    
            return view as! Self;
        }
        
        /// 添加一组表头
        func addTableColumn(titles: [String]) {
          for e in titles {
              let column = NSTableColumn.create(identifier: e, title: e)
              self.addTableColumn(column)
          }
        }
        
    }
    
    

    Github

    相关文章

      网友评论

        本文标题:MacOS 开发(六):NSTableView纯代码实现

        本文链接:https://www.haomeiwen.com/subject/csdxphtx.html