在UI布局时,添加一个尺寸不固定的NSTableView:(需放置于NSScrollView实例对象上)
其中NSTableView的代理方法——Delegate和DataSource:
//MARK:NSTableViewDelegate,NSTableViewDataSource
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
return 25.0
}
func numberOfRows(in tableView: NSTableView) -> Int {
return 36
}
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
var showStr = "第\(row)行"
if row%5 == 0 {
showStr = "adsasdasfda第\(row)行狗币键格的项吧goubigpp"
}
let attrDict = [NSAttributedString.Key.foregroundColor: NSColor.red]
let attrStr = NSAttributedString(string: showStr, attributes: attrDict)
return attrStr
}
func tableView(_ tableView: NSTableView, shouldEdit tableColumn: NSTableColumn?, row: Int) -> Bool {//可进行编辑
return true
}
func tableView(_ tableView: NSTableView, toolTipFor cell: NSCell, rect: NSRectPointer, tableColumn: NSTableColumn?, row: Int, mouseLocation: NSPoint) -> String {//鼠标悬停在cell上显示的提示文本
return "tip\n大厦等级拉丝机\n的撒酒疯拉萨附近阿里附近阿拉斯加阿克苏交电费卡拉时间反馈时间的考虑"
}
func tableView(_ tableView: NSTableView, shouldShowCellExpansionFor tableColumn: NSTableColumn?, row: Int) -> Bool {//当列表长度无法展示完整某行数据时 当鼠标悬停在此行上 是否扩展显示
return true
}
func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
print("tableColumn.title", tableColumn.title)
}
func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
print("row:", row)
return true
}
@objc func clickTableView() {
print("clickTableView")
}
(该NSTableView实例放置在ScrollView的documentView中)
-
方法1(
NSScrollView
->NSTableView
)
对该NSTableView实例对应的NSScrollView实例视图使用约束进行布局!(1层的约束)
func addTableViewJustScrollViewWithConstraint() {
let scrollV = NSScrollView()
self.view .addSubview(scrollV)
scrollV.hasVerticalRuler = true
scrollV.hasHorizontalScroller = true
scrollV.hasVerticalScroller = true
scrollV.translatesAutoresizingMaskIntoConstraints = false//添加约束
let scrollV_Lead = scrollV.leadingAnchor .constraint(equalTo: self.view.leadingAnchor, constant: 100.0)
let scrollV_Trail = scrollV.trailingAnchor .constraint(equalTo: self.view.trailingAnchor, constant: -100.0)
let scrollV_Top = scrollV.topAnchor .constraint(equalTo: self.view.topAnchor, constant: 20.0)
let scrollV_Bot = scrollV.bottomAnchor .constraint(equalTo: self.view.bottomAnchor, constant: -20)
let scrollV_consArr = [scrollV_Lead, scrollV_Trail, scrollV_Top, scrollV_Bot]
self.view .addConstraints(scrollV_consArr)
scrollV.wantsLayer = true
scrollV.layer?.backgroundColor = NSColor.red.cgColor
let tableV = NSTableView(frame: scrollV.bounds)//NSTableView()//是否设置尺寸无所谓——下方放置到ScrollView的documentView中
self.view .addSubview(tableV)
scrollV.contentView.documentView = tableV//scrollV.documentView//将TableView放置到ScrollView的documentView中
//不能添加约束——否则尺寸不能调整(宽不能调整)❌❌❌
//tableV.translatesAutoresizingMaskIntoConstraints = false//添加约束
//let tableV_Lead = tableV.leadingAnchor .constraint(equalTo: self.view.leadingAnchor, constant: 0.0)
//let tableV_Trail = tableV.trailingAnchor .constraint(equalTo: self.view.trailingAnchor, constant: 0.0)
//let tableV_Top = tableV.topAnchor .constraint(equalTo: self.view.topAnchor, constant: 0.0)
//let tableV_Bot = tableV.bottomAnchor .constraint(equalTo: self.view.bottomAnchor, constant: 0.0)
//let tableV_consArr = [tableV_Lead, tableV_Trail, tableV_Top, tableV_Bot]
//self.view .addConstraints(tableV_consArr)
tableV.wantsLayer = true
tableV.layer?.backgroundColor = NSColor.cyan.cgColor
tableV.delegate = self
tableV.dataSource = self
tableV.target = self; tableV.action = #selector(clickTableView)
//添加列(必须)
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Colum-1"))
column.width = 150//当前宽度
column.minWidth = 100//最小宽度
column.maxWidth = 200//最大宽度
column.title = "column-No.1"
column.headerToolTip = column.title
column.resizingMask = NSTableColumn.ResizingOptions.userResizingMask
tableV .addTableColumn(column)
let column2 = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Colum-2"))
column2.width = 150//当前宽度
column2.minWidth = 100//最小宽度
column2.maxWidth = 200//最大宽度
column2.title = "column-No.2"
column2.headerToolTip = column2.title
column2.resizingMask = NSTableColumn.ResizingOptions.userResizingMask
tableV .addTableColumn(column2)
}
UI层次结构:NSScrollView
-> NSTableView
-
方法2(
NSView
->NSScrollView
->NSTableView
)
对该NSTableView实例对应的NSScrollView实例视图使用约束进行布局!对NSScrollView实例视图对应(父视图)NSView实例视图使用约束进行布局!(2层的约束)
func addTableViewByOntoNSView() {
let leftV = NSView()
self.view .addSubview(leftV)
leftV.translatesAutoresizingMaskIntoConstraints = false//添加约束
let leftV_Lead = leftV.leadingAnchor .constraint(equalTo: self.view.leadingAnchor, constant: 100.0)
let leftV_Trail = leftV.trailingAnchor .constraint(equalTo: self.view.trailingAnchor, constant: -100.0)
let leftV_Top = leftV.topAnchor .constraint(equalTo: self.view.topAnchor, constant: 20.0)
let leftV_Bot = leftV.bottomAnchor .constraint(equalTo: self.view.bottomAnchor, constant: -20)
let leftV_consArr = [leftV_Lead, leftV_Trail, leftV_Top, leftV_Bot]
self.view .addConstraints(leftV_consArr)
leftV.wantsLayer = true
leftV.layer?.backgroundColor = NSColor.blue.cgColor
leftV.layer?.borderWidth = 3.0
leftV.layer?.borderColor = NSColor.black.cgColor
let scrollV = NSScrollView(frame: leftV.bounds)//NSScrollView()//是否设置尺寸无所谓——下方会添加约束
leftV .addSubview(scrollV)
scrollV.hasVerticalRuler = true
scrollV.hasHorizontalScroller = true
scrollV.hasVerticalScroller = true
//需要添加约束——否则不会添加上ScrollView滑动视图
scrollV.translatesAutoresizingMaskIntoConstraints = false//添加约束
let scrollV_Lead = scrollV.leadingAnchor .constraint(equalTo: leftV.leadingAnchor, constant: 0.0)
let scrollV_Trail = scrollV.trailingAnchor .constraint(equalTo: leftV.trailingAnchor, constant: 0.0)
let scrollV_Top = scrollV.topAnchor .constraint(equalTo: leftV.topAnchor, constant: 0.0)
let scrollV_Bot = scrollV.bottomAnchor .constraint(equalTo: leftV.bottomAnchor, constant: 0.0)
let scrollV_consArr = [scrollV_Lead, scrollV_Trail, scrollV_Top, scrollV_Bot]
leftV .addConstraints(scrollV_consArr)
scrollV.wantsLayer = true
scrollV.layer?.backgroundColor = NSColor.red.cgColor
let tableV = NSTableView(frame: leftV.bounds)//NSTableView()//是否设置尺寸无所谓——下方放置到ScrollView的documentView中
leftV .addSubview(tableV)
scrollV.contentView.documentView = tableV//scrollV.documentView
//不能添加约束——否则尺寸不能调整(宽不能调整)❌❌❌
//tableV.translatesAutoresizingMaskIntoConstraints = false//添加约束
//let tableV_Lead = tableV.leadingAnchor .constraint(equalTo: leftV.leadingAnchor, constant: 0.0)
//let tableV_Trail = tableV.trailingAnchor .constraint(equalTo: leftV.trailingAnchor, constant: 0.0)
//let tableV_Top = tableV.topAnchor .constraint(equalTo: leftV.topAnchor, constant: 0.0)
//let tableV_Bot = tableV.bottomAnchor .constraint(equalTo: leftV.bottomAnchor, constant: 0.0)
//let tableV_consArr = [tableV_Lead, tableV_Trail, tableV_Top, tableV_Bot]
//leftV .addConstraints(tableV_consArr)
tableV.wantsLayer = true
tableV.layer?.backgroundColor = NSColor.cyan.cgColor
tableV.delegate = self
tableV.dataSource = self
tableV.target = self; tableV.action = #selector(clickTableView)
//添加列(必须)
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Colum-1"))
column.width = 150//当前宽度
column.minWidth = 100//最小宽度
column.maxWidth = 200//最大宽度
column.title = "column-No.1"
column.headerToolTip = column.title
column.resizingMask = NSTableColumn.ResizingOptions.userResizingMask
tableV .addTableColumn(column)
let column2 = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Colum-2"))
column2.width = 150//当前宽度
column2.minWidth = 100//最小宽度
column2.maxWidth = 200//最大宽度
column2.title = "column-No.2"
column2.headerToolTip = column2.title
column2.resizingMask = NSTableColumn.ResizingOptions.userResizingMask
tableV .addTableColumn(column2)
}
UI层次结构:NSView
-> NSScrollView
-> NSTableView
-
方法3(
NSSplitView
->NSScrollView
->NSTableView
)
对该NSTableView实例对应的NSScrollView实例视图使用约束进行布局!对NSScrollView实例视图对应(父视图)NSSplitView实例视图 可以使用(也可以不使用) 约束进行布局!(1层(或2层)的约束)
(NSSplitView(分屏视图) — 默认会被子视图填充满 🔷🔷🔷)
func addTableViewByOntoNSSplitView() {
let leftV = NSSplitView()
self.view .addSubview(leftV)
leftV.translatesAutoresizingMaskIntoConstraints = false//添加约束
let leftV_Lead = leftV.leadingAnchor .constraint(equalTo: self.view.leadingAnchor, constant: 100.0)
let leftV_Trail = leftV.trailingAnchor .constraint(equalTo: self.view.trailingAnchor, constant: -100.0)
let leftV_Top = leftV.topAnchor .constraint(equalTo: self.view.topAnchor, constant: 20.0)
let leftV_Bot = leftV.bottomAnchor .constraint(equalTo: self.view.bottomAnchor, constant: -20)
let leftV_consArr = [leftV_Lead, leftV_Trail, leftV_Top, leftV_Bot]
self.view .addConstraints(leftV_consArr)
leftV.wantsLayer = true
leftV.layer?.backgroundColor = NSColor.blue.cgColor
leftV.layer?.borderWidth = 3.0
leftV.layer?.borderColor = NSColor.black.cgColor
let scrollV = NSScrollView(frame: leftV.bounds)//NSScrollView()//是否设置尺寸无所谓——添加到NSSplitView上
leftV .addSubview(scrollV)
scrollV.hasVerticalRuler = true
scrollV.hasHorizontalScroller = true
scrollV.hasVerticalScroller = true
//可添加也可不添加约束——都会添加上ScrollView滑动视图🔷🔷🔷
//scrollV.translatesAutoresizingMaskIntoConstraints = false//添加约束
//let scrollV_Lead = scrollV.leadingAnchor .constraint(equalTo: leftV.leadingAnchor, constant: 0.0)
//let scrollV_Trail = scrollV.trailingAnchor .constraint(equalTo: leftV.trailingAnchor, constant: 0.0)
//let scrollV_Top = scrollV.topAnchor .constraint(equalTo: leftV.topAnchor, constant: 0.0)
//let scrollV_Bot = scrollV.bottomAnchor .constraint(equalTo: leftV.bottomAnchor, constant: 0.0)
//let scrollV_consArr = [scrollV_Lead, scrollV_Trail, scrollV_Top, scrollV_Bot]
//leftV .addConstraints(scrollV_consArr)
scrollV.wantsLayer = true
scrollV.layer?.backgroundColor = NSColor.red.cgColor
let tableV = NSTableView(frame: leftV.bounds)//NSTableView()//是否设置尺寸无所谓——下方放置到ScrollView的documentView中
leftV .addSubview(tableV)
scrollV.contentView.documentView = tableV//scrollV.documentView
//不能添加约束——否则尺寸不能调整(宽不能调整)❌❌❌
//tableV.translatesAutoresizingMaskIntoConstraints = false//添加约束
//let tableV_Lead = tableV.leadingAnchor .constraint(equalTo: leftV.leadingAnchor, constant: 0.0)
//let tableV_Trail = tableV.trailingAnchor .constraint(equalTo: leftV.trailingAnchor, constant: 0.0)
//let tableV_Top = tableV.topAnchor .constraint(equalTo: leftV.topAnchor, constant: 0.0)
//let tableV_Bot = tableV.bottomAnchor .constraint(equalTo: leftV.bottomAnchor, constant: 0.0)
//let tableV_consArr = [tableV_Lead, tableV_Trail, tableV_Top, tableV_Bot]
//leftV .addConstraints(tableV_consArr)
tableV.wantsLayer = true
tableV.layer?.backgroundColor = NSColor.cyan.cgColor
tableV.delegate = self
tableV.dataSource = self
tableV.target = self; tableV.action = #selector(clickTableView)
//添加列(必须)
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Colum-1"))
column.width = 150//当前宽度
column.minWidth = 100//最小宽度
column.maxWidth = 200//最大宽度
column.title = "column-No.1"
column.headerToolTip = column.title
column.resizingMask = NSTableColumn.ResizingOptions.userResizingMask
tableV .addTableColumn(column)
let column2 = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Colum-2"))
column2.width = 150//当前宽度
column2.minWidth = 100//最小宽度
column2.maxWidth = 200//最大宽度
column2.title = "column-No.2"
column2.headerToolTip = column2.title
column2.resizingMask = NSTableColumn.ResizingOptions.userResizingMask
tableV .addTableColumn(column2)
}
UI层次结构:NSSplitView
-> NSScrollView
-> NSTableView
参考资料:
NSLayoutConstraint
NSSplitView | NSSplitViewController | NSSplitViewDelegate
NSSplitView and autolayout