来源列表
什么是来源清单?它是NSOutlineView
(这是{
NSTableView
)和特殊
治疗。 Finder屏幕截图:
要创建来源列表,您要做的就是设置
selectionHighlightStyle
属性
.sourceList
。该文档说:
NSTableView的源列表样式。在10.5上,浅蓝色渐变用于突出显示选定的行。
它的作用是什么?在Xcode中跳转到定义并阅读注释(文档中未包含):
NSTableView的源列表样式。在10.10及更高版本上,模糊选择用于突出显示行。在此之前,使用了浅蓝色渐变。注意:具有drawsBackground属性的单元格应将其设置为NO。否则,它们将覆盖NSTableView所做的突出显示。设置此样式将具有将背景色设置为“源列表”背景色的副作用。此外,在NSOutlineView中,更改了以下属性以获取标准的“源列表”外观:indentationPerLevel,rowHeight和intercellSpacing。调用setSelectionHighlightStyle之后:可以根据需要更改其他任何属性。在10.11中,如果将背景颜色从“源列表”背景颜色更改为其他颜色,则该表将不再以源列表模糊样式来绘制所选内容,而是将正常显示为蓝色。
由于您位于大苏尔,因此请注意SelectionHighlightStyle.sourceList
已过时。
一个人应该使用style
&effectiveStyle
。
示例项目
Xcode:
- 新项目
- macOS和App(Storyboard和AppKit App委托和Swift)
- Main.storyboard
- 添加源列表控件
- 位置和修复约束
- 将委托和数据源设置为ViewController
- 启用自动保存展开的项目
- 将“自动保存”设置为您想要的任何值(我在那里有
FinderLikeSidebar
)
- 明智地选择,因为扩展状态保存在用户默认设置中
在
NSOutlineView Items FinderLikeSidebar
键下
- 创建
@IBOutlet var outlineView: NSOutlineView!
- 添加另一个文本表单元格视图(无图像)
- ViewController.swift
屏幕截图
如您所见,它几乎像Finder一样-第二级仍然缩进。原因
这是因为 Documents 节点是可扩展的(有子级)。我有它们在这里演示自动保存。
如果要将所有第二级节点移到左侧,只需删除它们。
ViewController.swift代码
关于它,除了-没有其他要说的内容,请阅读评论:)
import Cocoa
// Sample Node class covering groups & regular items
class Node {
let id: Int
let title: String
let symbolName: String?
let children: [Node]
let isGroup: Bool
init(id: Int,title: String,symbolName: String? = nil,children: [Node] = [],isGroup: Bool = false) {
self.id = id
self.title = title
self.symbolName = symbolName
self.children = children
self.isGroup = isGroup
}
convenience init(groupId: Int,children: [Node]) {
self.init(id: groupId,title: title,children: children,isGroup: true)
}
}
extension Node {
var cellIdentifier: NSUserInterfaceItemIdentifier {
// These must match identifiers in Main.storyboard
NSUserInterfaceItemIdentifier(rawValue: isGroup ? "GroupCell" : "DataCell")
}
}
extension Array where Self.Element == Node {
// Search for a node (recursively) until a matching element is found
func firstNode(where predicate: (Element) throws -> Bool) rethrows -> Element? {
for element in self {
if try predicate(element) {
return element
}
if let matched = try element.children.firstNode(where: predicate) {
return matched
}
}
return nil
}
}
class ViewController: NSViewController,NSOutlineViewDelegate,NSOutlineViewDataSource {
@IBOutlet var outlineView: NSOutlineView!
let data = [
Node(groupId: 1,title: "Favorites",children: [
Node(id: 11,title: "AirDrop",symbolName: "wifi"),Node(id: 12,title: "Recents",symbolName: "clock"),Node(id: 13,title: "Applications",symbolName: "hammer")
]),Node(groupId: 2,title: "iCloud",children: [
Node(id: 21,title: "iCloud Drive",symbolName: "icloud"),Node(id: 22,title: "Documents",symbolName: "doc",children: [
Node(id: 221,title: "Work",symbolName: "folder"),Node(id: 221,title: "Personal",symbolName: "folder.badge.person.crop"),])
]),]
override func viewWillAppear() {
super.viewWillAppear()
// Expanded items are saved in the UserDefaults under the key:
//
// "NSOutlineView Items \(autosaveName)"
//
// By default,this value is not present. When you expand some nodes,// an array with persistent objects is saved. When you collapse all nodes,// the array is removed from the user defaults (not an empty array,// but back to nil = removed).
//
// IOW there's no way to check if user already saw this source list,// modified expansion state,etc. We will use custom key for this
// purpose,so we can expand group nodes (top level) when the source
// list is displayed for the first time.
//
// Next time,we wont expand anything and will honor autosaved expanded
// items.
if UserDefaults.standard.object(forKey: "FinderLikeSidebarAppeared") == nil {
data.forEach {
outlineView.expandItem($0)
}
UserDefaults.standard.set(true,forKey: "FinderLikeSidebarAppeared")
}
}
// Number of children or groups (item == nil)
func outlineView(_ outlineView: NSOutlineView,numberOfChildrenOfItem item: Any?) -> Int {
item == nil ? data.count : (item as! Node).children.count
}
// Child of a node or group (item == nil)
func outlineView(_ outlineView: NSOutlineView,child index: Int,ofItem item: Any?) -> Any {
item == nil ? data[index] : (item as! Node).children[index]
}
// View for our node
func outlineView(_ outlineView: NSOutlineView,viewFor tableColumn: NSTableColumn?,item: Any) -> NSView? {
guard let node = item as? Node,let cell = outlineView.makeView(withIdentifier: node.cellIdentifier,owner: self) as? NSTableCellView else {
return nil
}
cell.textField?.stringValue = node.title
if !node.isGroup {
cell.imageView?.image = NSImage(systemSymbolName: node.symbolName ?? "folder",accessibilityDescription: nil)
}
return cell
}
// Mark top level items as group items
func outlineView(_ outlineView: NSOutlineView,isGroupItem item: Any) -> Bool {
(item as! Node).isGroup
}
// Every node is expandable if it has children
func outlineView(_ outlineView: NSOutlineView,isItemExpandable item: Any) -> Bool {
!(item as! Node).children.isEmpty
}
// Top level items (group items) are not selectable
func outlineView(_ outlineView: NSOutlineView,shouldSelectItem item: Any) -> Bool {
!(item as! Node).isGroup
}
// Object to save in the user defaults (NSOutlineView Items FinderLikeSidebar)
func outlineView(_ outlineView: NSOutlineView,persistentObjectForItem item: Any?) -> Any? {
(item as! Node).id
}
// Find an item from the saved object (NSOutlineView Items FinderLikeSidebar)
func outlineView(_ outlineView: NSOutlineView,itemForPersistentObject object: Any) -> Any? {
guard let id = object as? Int else { return nil }
return data.firstNode { $0.id == id }
}
}
本文链接:https://www.f2er.com/1864500.html