简体   繁体   English

NSTableView分组

[英]NSTableView grouping

I am trying to add groups to my NSTableView. 我正在尝试向我的NSTableView添加组。 I found this function: 我发现此功能:

func tableView(tableView: NSTableView, isGroupRow row: Int) -> Bool
{
    if (row == 5)
    {
        return true;
    }

    return false;
}

Which works great. 哪个很棒。 Row 5 is a group after that. 之后的第5行是一组。 But I am not sure how to add a title to that? 但是我不确定该如何添加标题?

I am using a custom view: 我正在使用自定义视图:

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
    if (row == 5)
    {
        ??????
    }

    let result : CellViewCustom = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! CellViewCustom

    ...

    return result
}

But in row 5 it says tableColumn == nil 但是在第5行中,它说tableColumn == nil

How do I add text to the group header? 如何在群组标题中添加文字?

Unlike in iOS, the group cells of NSTableView are "inline" in the (non-nested) data source array. 与iOS不同, NSTableView的组单元在(非嵌套的)数据源数组中是“内联”的。

Simple example 简单的例子

  • A custom struct Item with property name , title and isGroup . 具有属性nametitleisGroup自定义结构Item All instances whose title is not empty are group rows title不为空的所有实例均为组行

     struct Item { let name, title : String let isGroup : Bool init(name : String, title : String = "") { self.name = name self.title = title self.isGroup = !title.isEmpty } } 
  • Create a data source array and assign 2 group and 3 normal rows 创建一个数据源数组并分配2组和3条普通行

     var items = [Item]() ... items = [Item(name:"", title:"Group 1"), Item(name:"bar"), Item(name:"baz"), Item(name:"", title:"Group 2"), Item(name:"zab")] 
  • In isGroupRow just return the value of the property isGroupRow只返回属性的值

     func tableView(tableView: NSTableView, isGroupRow row: Int) -> Bool { return items[row].isGroup } 
  • In viewForTableColumn create two different views for group and table row viewForTableColumn为组和表行创建两个不同的视图

     func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? { let item = items[row] if item.isGroup { let groupCell = tableView.makeViewWithIdentifier("GroupCell", owner:self) as! NSTableCellView groupCell.textField!.stringValue = item.title return cell } else { let cell = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! CellViewCustom ... return cell } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM