以下创建tableView的编程方法适用于我的系统。在添加滚动视图之前,我无法看到tableView。可以通过创建Swift项目、添加名为“main.Swift”的新文件并将此代码复制/粘贴到XCode中来运行源代码。删除预先提供的AppDelegate以避免重复符号。我试着尽可能多地使用你的代码。
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDelegate, NSTableViewDataSource {
var window:NSWindow!
var shortcuts = [["name": "print", "key":"P", "modifier":"shift"],
["name": "copy", "key":"C", "modifier":"alt"],
["name": "cut", "key":"X", "modifier":"alt"],
["name": "paste", "key":"V", "modifier":"alt"]]
func numberOfRows(in tableView: NSTableView) -> Int {
return shortcuts.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let cellView = NSTableCellView(frame: NSRect(x: 0, y: 0, width: tableColumn!.width, height: tableView.rowHeight))
switch tableColumn?.identifier {
case NSUserInterfaceItemIdentifier("NameColumn"):
let textField = NSTextField(frame: cellView.bounds)
textField.stringValue = shortcuts[row]["name"]!
textField.isEditable = false
textField.isBezeled = false
textField.drawsBackground = false
textField.autoresizingMask = [.width, .height] // Resize with the cell view
cellView.addSubview(textField)
return cellView
case NSUserInterfaceItemIdentifier("HotkeyColumn"):
let textField = NSTextField(frame: cellView.bounds)
textField.stringValue = shortcuts[row]["key"]!
textField.isEditable = false
textField.isBezeled = false
textField.drawsBackground = false
textField.autoresizingMask = [.width, .height] // Resize with the cell view
cellView.addSubview(textField)
return cellView
default:
return nil
}
}
func buildMenu() {
let mainMenu = NSMenu()
NSApp.mainMenu = mainMenu
// **** App menu **** //
let appMenuItem = NSMenuItem()
mainMenu.addItem(appMenuItem)
let appMenu = NSMenu()
appMenuItem.submenu = appMenu
appMenu.addItem(withTitle: "Quit", action:#selector(NSApplication.terminate), keyEquivalent: "q")
}
func buildWnd() {
let _wndW : CGFloat = 400
let _wndH : CGFloat = 300
window = NSWindow(contentRect:NSMakeRect(0,0,_wndW,_wndH),styleMask:[.titled, .closable, .miniaturizable, .resizable], backing:.buffered, defer:false)
window.center()
window.title = "Swift TableView Demo"
window.makeKeyAndOrderFront(window)
// **** TableView **** //
let scrlView = NSScrollView(frame:NSMakeRect(10,10,380,240))
let tableView = NSTableView(frame:NSMakeRect(0,0,364,240))
tableView.delegate = self
tableView.dataSource = self
let nameColumn = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("NameColumn"))
nameColumn.title = "Name"
nameColumn.width = 100
tableView.addTableColumn(nameColumn)
let hotkeyColumn = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("HotkeyColumn"))
hotkeyColumn.title = "Hotkey"
hotkeyColumn.width = 200
tableView.addTableColumn(hotkeyColumn)
scrlView.documentView = tableView
window.contentView!.addSubview(scrlView)
// **** Quit btn **** //
let quitBtn = NSButton (frame:NSMakeRect( _wndW - 50, _wndH - 40, 40, 40 ))
quitBtn.bezelStyle = .circular
quitBtn.autoresizingMask = [.minXMargin,.maxYMargin]
quitBtn.title = "Q"
quitBtn.action = #selector(NSApplication.terminate)
window.contentView!.addSubview(quitBtn)
}
func applicationDidFinishLaunching(_ notification: Notification) {
buildMenu()
buildWnd()
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
}
let appDelegate = AppDelegate()
// **** main.swift **** //
let app = NSApplication.shared
app.delegate = appDelegate
app.setActivationPolicy(.regular)
app.activate(ignoringOtherApps:true)
app.run()