swift - 如何使用 Swift 在 NSAlert 的附件视图中创建 NSTableView
问题描述
我正在尝试运行一个 Swift 脚本,我希望一个简单的 NSAlert 包含一个
NSTableView
到目前为止我还不能让它工作:
#!/usr/bin/env swift
import AppKit
import Foundation
let app = NSApplication.shared
app.setActivationPolicy(.regular) // Magic to accept keyboard input and be docked!
class TableViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
var initialized = false
let scrollView = NSScrollView(frame: NSRect(x: 0, y: 4, width: 200, height: 500))
let tableView = NSTableView(frame: NSRect(x: 0, y: 4, width: 200, height: 500))
override func loadView() {
print("Load view")
self.view = NSView()
}
override func viewDidLoad() {
print("Didweload")
super.viewDidLoad()
}
override func viewDidLayout() {
print("viewDidLayout")
if !initialized {
initialized = true
setupView()
setupTableView()
}
}
func setupView() {
print("setupView")
self.view.translatesAutoresizingMaskIntoConstraints = false
// self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200))
}
func setupTableView() {
print("setupTableView")
self.view.addSubview(scrollView)
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
// self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1.0, constant: 0))
// self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 23))
// self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1.0, constant: 0))
// self.view.addConstraint(NSLayoutConstraint(item: self.scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 0))
tableView.frame = scrollView.bounds
tableView.delegate = self
tableView.dataSource = self
tableView.headerView = nil
scrollView.backgroundColor = NSColor.clear
scrollView.drawsBackground = false
tableView.backgroundColor = NSColor.clear
tableView.appearance = NSAppearance(named: NSAppearance.Name.vibrantDark)
let col = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "col"))
col.minWidth = 200
tableView.addTableColumn(col)
scrollView.documentView = tableView
scrollView.hasHorizontalScroller = false
scrollView.hasVerticalScroller = true
}
func numberOfRows(in tableView: NSTableView) -> Int {
return 100
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
print("tableView")
let text = NSTextField()
text.stringValue = "Hello World"
let cell = NSTableCellView()
cell.addSubview(text)
text.drawsBackground = false
text.isBordered = false
text.translatesAutoresizingMaskIntoConstraints = false
// 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? {
print("tableView")
let rowView = NSTableRowView()
rowView.isEmphasized = false
return rowView
}
}
func runOptionsDialog(args: [String]) {
let title = args[0]
let text = args[1]
let okButton = args[2]
let cancelButton = args[3]
let a = NSAlert()
a.messageText = title
a.alertStyle = NSAlert.Style.warning
a.informativeText = text
a.addButton(withTitle: okButton)
a.addButton(withTitle: cancelButton)
let con = TableViewController()
con.tableView.reloadData()
a.accessoryView = con.view
app.activate(ignoringOtherApps: true)
a.runModal()
}
runOptionsDialog(args:["title", "whast", "ok", "cancel"])
我用swift table.swift
.
解决方案
推荐阅读
- node.js - Firebase 云存储做一个通配符搜索 Nodejs
- powershell - 我可以在不安装的情况下使用 PowerShell 模块吗?
- r - R中的流失率
- opencv - 如何在频域中旋转非平方图像
- linux - Linux setxattr:可以使用 Unicode 字符串吗?
- amazon-web-services - 如何将 Docker 映像从 DockerHub 拉到 Elastic Beanstalk?
- python - Python上的Excel数据处理
- javascript - 如何在节点 js 中编写非阻塞 for 循环
- python - 在 pymunk 群众之间产生排斥力
- python - 澄清文件路径的术语