简体   繁体   English

NSOutlineView,使用项目:AnyObject

[英]NSOutlineView, using item: AnyObject

I'm creating a NSOutlineView . 我正在创建一个NSOutlineView When implementing the Data Source, although I'm able to create the top hierarchy I can not implement the childHierarchy. 在实现数据源时,尽管我能够创建顶层层次结构,但无法实现childHierarchy。 The reason is that I can't read the item: AnyObject? 原因是我无法阅读该项目:AnyObject? which prevents me from returning the right array from the dictionary. 这使我无法从字典中返回正确的数组。

 //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
    }

}

} }

I used this as a reference, although it's Objective-C implemented 尽管已实现 Objective-C,但我还是以此为参考

You need to cast the item to the correct type for your outline. 您需要将item转换为适合您轮廓的正确类型。 Generally you'd want to use a real data model, but for your toy problem with exactly two levels in the hierarchy, this suffices: 通常,您希望使用一个真实的数据模型,但是对于层次结构中恰好有两个级别的玩具问题,就足够了:

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

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

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