簡體   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