繁体   English   中英

NSTableView分组

[英]NSTableView grouping

我正在尝试向我的NSTableView添加组。 我发现此功能:

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

    return false;
}

哪个很棒。 之后的第5行是一组。 但是我不确定该如何添加标题?

我正在使用自定义视图:

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
}

但是在第5行中,它说tableColumn == nil

如何在群组标题中添加文字?

与iOS不同, NSTableView的组单元在(非嵌套的)数据源数组中是“内联”的。

简单的例子

  • 具有属性nametitleisGroup自定义结构Item 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 } } 
  • 创建一个数据源数组并分配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")] 
  • isGroupRow只返回属性的值

     func tableView(tableView: NSTableView, isGroupRow row: Int) -> Bool { return items[row].isGroup } 
  • 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