如何用粗体显示 NSTableView 的某些特定项目?

How to make some specific items of a NSTableView in bold?

我想将不可编辑的、基于视图的 NSTableView 的一些项目设置为粗体。这些项目对应于我用来填充 TableView 的数组的特定索引号。

我想在向用户显示 NSTableView 之前设置更改。

我试图用这种方法处理这个变化,但我找不到办法:

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any?

如果您不想使用 Cocoa 绑定

这与您在 iOS 上的操作方式非常相似。在 tableView(_:viewFor:row:)

中配置单元格的视图
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
    @IBOutlet weak var tableView: NSTableView!

    var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    var boldDays = ["Monday", "Wednesday"]

    override func viewDidLoad() {
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

    func numberOfRows(in tableView: NSTableView) -> Int {
        return daysOfWeek.count
    }

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        // Assuming that you have set the cell view's Identifier in Interface Builder
        let cell = tableView.make(withIdentifier: "myCell", owner: self) as! NSTableCellView
        let day = daysOfWeek[row]

        cell.textField?.stringValue = day
        if boldDays.contains(day) {
            let fontSize = NSFont.systemFontSize()
            cell.textField?.font = NSFont.boldSystemFont(ofSize: fontSize)

            // if you require more extensive styling, it may be better to use NSMutableAttributedString
        }

        return cell
    }   
}

如果你想使用Cocoa绑定

Cocoa 绑定可以让这一切变得非常简单,但如果你设置了最细微的错误,就很难弄清楚哪里出错了。注意 warning from Apple:

Populating a table view using Cocoa bindings is considered an advanced topic. Although using bindings requires significantly less code—in some cases no code at all—the bindings are hard to see and debug if you aren’t familiar with the interface. It’s strongly suggested that you become comfortable with the techniques for managing table views programmatically before you decide to use Cocoa bindings.

无论如何,这是如何做到的。一、代码:

// The data model must inherit from NSObject for KVO compliance
class WeekDay : NSObject {
    var name: String
    var isBold: Bool

    init(name: String, isBold: Bool = false) {
        self.name = name
        self.isBold = isBold
    }
}

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
    @IBOutlet weak var tableView: NSTableView!

    let daysOfWeek = [
        WeekDay(name: "Sunday"),
        WeekDay(name: "Monday", isBold: true),
        WeekDay(name: "Tuesday"),
        WeekDay(name: "Wednesday", isBold: true),
        WeekDay(name: "Thursday"),
        WeekDay(name: "Friday"),
        WeekDay(name: "Saturday")
    ]

    override func viewDidLoad() {
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }
}

然后是 Interface Builder 配置:

  1. 确保 Table 列 Table 单元格视图 具有相同的标识符。最好是将它们都留空以获得自动

  1. Select Table View, bind Table Content to View Controller, Model Key Path = self.daysOfWeek

  1. Select Table View Cell,将 Value 绑定到 Table Cell View(不是开玩笑),Model Key Path = objectValue.name

  1. 向下滚动,将字体粗体绑定到 Table 单元格视图,模型键路径 = objectValue.isBold


无论哪种方式,你最终应该得到这样的结果:

根据需要进行润色。