首页 > 解决方案 > 如何使用 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.

输出

标签: swiftmacos

解决方案


推荐阅读