mirror of
https://github.com/yJason/ClashX-Dashboard.git
synced 2026-02-04 10:02:26 +08:00
misc: extension createCellView
This commit is contained in:
@@ -173,7 +173,7 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
||||
|
||||
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
||||
|
||||
guard let cellView = createCellView(tableView),
|
||||
guard let cellView = tableView.createCellView(with: "ConnsTableCellView"),
|
||||
let s = tableColumn?.identifier.rawValue.split(separator: ".").last,
|
||||
let tc = TableColumn(rawValue: String(s))
|
||||
else { return nil }
|
||||
@@ -215,52 +215,7 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
||||
tableView.reloadData()
|
||||
}
|
||||
|
||||
func createCellView(_ tableView: NSTableView) -> NSTableCellView? {
|
||||
// https://stackoverflow.com/a/27624927
|
||||
|
||||
var cellView: NSTableCellView?
|
||||
if let spareView = tableView.makeView(withIdentifier: .init("ConnsTableCellView"),
|
||||
owner: self) as? NSTableCellView {
|
||||
|
||||
// We can use an old cell - no need to do anything.
|
||||
cellView = spareView
|
||||
|
||||
} else {
|
||||
|
||||
// Create a text field for the cell
|
||||
let textField = NSTextField()
|
||||
textField.backgroundColor = NSColor.clear
|
||||
textField.translatesAutoresizingMaskIntoConstraints = false
|
||||
textField.isBordered = false
|
||||
textField.font = .systemFont(ofSize: 13)
|
||||
|
||||
// Create a cell
|
||||
let newCell = NSTableCellView()
|
||||
newCell.identifier = .init("ConnsTableCellView")
|
||||
newCell.addSubview(textField)
|
||||
newCell.textField = textField
|
||||
|
||||
// Constrain the text field within the cell
|
||||
newCell.addConstraints(
|
||||
NSLayoutConstraint.constraints(withVisualFormat: "H:|[textField]|",
|
||||
options: [],
|
||||
metrics: nil,
|
||||
views: ["textField" : textField]))
|
||||
|
||||
newCell.addConstraint(.init(item: textField, attribute: .centerY, relatedBy: .equal, toItem: newCell, attribute: .centerY, multiplier: 1, constant: 0))
|
||||
|
||||
|
||||
textField.bind(NSBindingName.value,
|
||||
to: newCell,
|
||||
withKeyPath: "objectValue",
|
||||
options: nil)
|
||||
|
||||
cellView = newCell
|
||||
}
|
||||
|
||||
return cellView
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,4 +268,52 @@ extension NSTableView {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func createCellView(with identifier: String) -> NSTableCellView? {
|
||||
// https://stackoverflow.com/a/27624927
|
||||
|
||||
var cellView: NSTableCellView?
|
||||
if let spareView = makeView(withIdentifier: .init(identifier),
|
||||
owner: self) as? NSTableCellView {
|
||||
|
||||
// We can use an old cell - no need to do anything.
|
||||
cellView = spareView
|
||||
|
||||
} else {
|
||||
|
||||
// Create a text field for the cell
|
||||
let textField = NSTextField()
|
||||
textField.backgroundColor = NSColor.clear
|
||||
textField.translatesAutoresizingMaskIntoConstraints = false
|
||||
textField.isBordered = false
|
||||
textField.font = .systemFont(ofSize: 13)
|
||||
textField.lineBreakMode = .byTruncatingTail
|
||||
|
||||
// Create a cell
|
||||
let newCell = NSTableCellView()
|
||||
newCell.identifier = .init(identifier)
|
||||
newCell.addSubview(textField)
|
||||
newCell.textField = textField
|
||||
|
||||
// Constrain the text field within the cell
|
||||
newCell.addConstraints(
|
||||
NSLayoutConstraint.constraints(withVisualFormat: "H:|[textField]|",
|
||||
options: [],
|
||||
metrics: nil,
|
||||
views: ["textField" : textField]))
|
||||
|
||||
newCell.addConstraint(.init(item: textField, attribute: .centerY, relatedBy: .equal, toItem: newCell, attribute: .centerY, multiplier: 1, constant: 0))
|
||||
|
||||
|
||||
textField.bind(NSBindingName.value,
|
||||
to: newCell,
|
||||
withKeyPath: "objectValue",
|
||||
options: nil)
|
||||
|
||||
cellView = newCell
|
||||
}
|
||||
|
||||
return cellView
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user