swift - NSOutlineView,使用项目 : AnyObject

标签 swift macos nsoutlineview

我正在创建一个NSOutlineView。实现数据源时,虽然我能够创建顶层层次结构,但无法实现子层次结构。原因是我无法读取 item: AnyObject? 这阻止我从字典中返回正确的数组。

 //MARK: NSOutlineView
var outlineTopHierarchy = ["COLLECT", "REVIEW", "PROJECTS", "AREAS"]
var outlineContents = ["COLLECT":["a","b"], "REVIEW":["c","d"],"PROJECTS":["e","f"],"AREAS":["g","h"]]

//Get the children for item
func childrenForItem (itemPassed : AnyObject?) -> Array<String>{
    var childrenResult = Array<String>()
    if(itemPassed == nil){ //If no item passed we return the highest level of hirarchy
        childrenResult = outlineTopHierarchy
    }else{ 


        //ISSUE HERE:
        //NEED TO FIND ITS TITLE to call the correct child 
        childrenResult = outlineContents["COLLECT"]!  //FAKED, should be showing the top hierarchy item so I could return the right data


    }
    return childrenResult
}


//Data source
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject{
    return childrenForItem(item)[index]
}

func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool{
    if(outlineView.parentForItem(item) == nil){
        return true
    }else{
        return false
    }
}

func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int{
    return childrenForItem(item).count
}


func outlineView(outlineView: NSOutlineView, viewForTableColumn: NSTableColumn?, item: AnyObject) -> NSView? {

    // For the groups, we just return a regular text view.
    if (outlineTopHierarchy.contains(item as! String)) {
        let resultTextField = outlineView.makeViewWithIdentifier("HeaderCell", owner: self) as! NSTableCellView
        resultTextField.textField!.stringValue = item as! String
        return resultTextField
    }else{
        // The cell is setup in IB. The textField and imageView outlets are properly setup.
        let resultTextField = outlineView.makeViewWithIdentifier("DataCell", owner: self) as! NSTableCellView
        resultTextField.textField!.stringValue = item as! String
        return resultTextField
    }

}

}

我用它作为引用,尽管它是 Objective-C implemented

最佳答案

您需要将item转换为大纲的正确类型。通常,您希望使用真实的数据模型,但对于层次结构中只有两个级别的玩具问题,这就足够了:

func childrenForItem (itemPassed : AnyObject?) -> Array<String>{
    if let item = itemPassed {
        let item = item as! String
        return outlineContents[item]!
    } else {
        return outlineTopHierarchy
    }
}

关于swift - NSOutlineView,使用项目 : AnyObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463991/

相关文章:

objective-c - 递归删除树目录结构中的空项

swift - 从 CoreData 刷新 NSTreeController

swift - expectationForPredicate 失败测试用例

c - Mac 和 Linux 的 Authenticode 或其他代码签名

macos - Mac OS GCC for C 中定义了什么?

macos - 将Ctrl-C发送到LLDB中的应用

cocoa - 将文件拖放到 NSOutlineView 中

ios - Swift Facebook SDK 注销

ios - Facebook ios sdk(obj-c to swift)问题

swift - 如何使用 swift 在 iOS 中打开定位服务页面?