【Swift Mac开发】源代码创建NSTableView(含自定义NSTableViewCellView)
1、简易的tableView
//纯代码创建tableView,使用NSLayoutConstraint添加约束进行布局 import Cocoa class JFSimpleTableViewController: NSViewController { var scrollView : NSScrollView! var tableView : NSTableView! override func viewDidLoad() { super.viewDidLoad() scrollView = NSScrollView(frame: self.view.bounds) self.view.addSubview(scrollView) tableView = NSTableView.init(frame: CGRect(x: 0, y: 0, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)) tableView.delegate = self tableView.dataSource = self let firstCol = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "firstCol")) firstCol.width = 200 firstCol.title = "第一列" tableView.addTableColumn(firstCol) let secondCol = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "secondCol")) secondCol.width = scrollView.bounds.size.width - 200 secondCol.title = "第二列" tableView.addTableColumn(secondCol) scrollView.documentView = tableView } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } } extension JFSimpleTableViewController : NSTableViewDelegate,NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { return 10 } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { let text = NSTextField() if tableColumn?.identifier == NSUserInterfaceItemIdentifier("firstCol") { text.stringValue = "\(row)" } else { text.stringValue = "Hello World" } let cell = NSTableCellView() cell.addSubview(text) text.drawsBackground = false text.isBordered = false text.translatesAutoresizingMaskIntoConstraints = false //方法1:使用SnapKit进行布局 // text.snp.makeConstraints { (make) in // make.centerY.equalTo(text) // make.left.equalTo(13) // make.right.equalTo(-13) // } //方法2:使用AutoLayout自布局 cell.addConstraint(NSLayoutConstraint(item: text, attribute: .centerY, relatedBy: .equal, toItem: cell, attribute: .centerY, multiplier: 1, constant: 0)) cell.addConstraint(NSLayoutConstraint(item: text, attribute: .left, relatedBy: .equal, toItem: cell, attribute: .left, multiplier: 1, constant: 13)) cell.addConstraint(NSLayoutConstraint(item: text, attribute: .right, relatedBy: .equal, toItem: cell, attribute: .right, multiplier: 1, constant: -13)) return cell } func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { let rowView = NSTableRowView() rowView.isEmphasized = false return rowView } }
2、自定义NSTableCellView
import Cocoa class JFTableViewCell: NSTableCellView { var picImageView : NSImageView! var titleLab : NSTextField! override init(frame frameRect: NSRect) { super.init(frame: frameRect) configureView() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } func configureView() { picImageView = NSImageView() picImageView.wantsLayer = true picImageView.layer?.backgroundColor = NSColor.red.cgColor self.addSubview(picImageView) titleLab = NSTextField() titleLab.textColor = NSColor.black titleLab.stringValue = "custom Cell" titleLab.drawsBackground = false titleLab.isBordered = false titleLab.translatesAutoresizingMaskIntoConstraints = false self.addSubview(titleLab) picImageView.snp.makeConstraints { (make) in make.left.equalTo(10) make.centerY.equalToSuperview() make.width.height.equalTo(30) } titleLab.snp.makeConstraints { (make) in make.left.equalTo(picImageView.snp.right).offset(10) make.centerY.equalTo(picImageView) make.height.equalTo(20) make.right.equalTo(-10) } } }
import Cocoa import SnapKit class ViewController: NSViewController { var scrollView : NSScrollView! var tableView : NSTableView! override func viewDidLoad() { super.viewDidLoad() scrollView = NSScrollView(frame: self.view.bounds) self.view.addSubview(scrollView) tableView = NSTableView.init(frame: CGRect(x: 0, y: 0, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)) tableView.delegate = self tableView.dataSource = self let firstCol = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "firstCol")) firstCol.width = 200 firstCol.title = "第一列" tableView.addTableColumn(firstCol) let secondCol = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "secondCol")) secondCol.width = scrollView.bounds.size.width - 200 secondCol.title = "第二列" tableView.addTableColumn(secondCol) scrollView.documentView = tableView } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } } extension ViewController : NSTableViewDelegate,NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { return 10 } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { var frame = NSRect.zero if tableColumn?.identifier == NSUserInterfaceItemIdentifier("firstCol") { frame = NSRect(x: 0, y: 0, width: 200, height: 30) } else { frame = NSRect(x: 0, y: 0, width: self.scrollView.bounds.width - 200, height: 30) } let cellView = JFTableViewCell(frame: frame) if tableColumn?.identifier == NSUserInterfaceItemIdentifier("firstCol") { cellView.titleLab.stringValue = "\(row)" } else { cellView.titleLab.stringValue = "test" } return cellView } func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { let rowView = NSTableRowView() rowView.isEmphasized = false return rowView } }