Because of the system mechanism, pure code implementing the scrollView must implement the scrollView itself, as follows:
🌰🌰 : - (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 the width. The equalTo (self. The view). MultipliedBy (0.5), and make the bottom. The equalTo (self. The view). Offset (0); }]; } -(NNTableView *)tableView{if (! _tableView) { _tableView = ({ NNTableView *view = [NNTableView create:CGRectZero]; view.headerView = nil; view.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone; / / highlight style / / the 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; // Automatically hide the scroll bar (when scrolling) view; }); } return _tableView; }Copy the code
View encapsulation:
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 } }Copy the code
@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 gains focus when the style of the selectionHighlightStyle =. The regular/line/highlight the style of the layer? . BackgroundColor = NSColor. Background. CgColor the usesAlternatingRowBackgroundColors = false / / background color, a line of white, gray. After setting, the backgroundColor of the original setting is invalid. // view.gridColor = NSColor.red view.appearance = NSAppearance(named: .aqua) // view.headerView = nil; view.rowHeight = 70; return view as! Self; } // add a set of headers func addTableColumn(titles: [String]) {for e in titles {let column = NSTableColumn. Create (identifier: e, title: e) self.addTableColumn(column) } } }Copy the code
Github