首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSOutlineView,使用项目: AnyObject

NSOutlineView,使用项目: AnyObject
EN

Stack Overflow用户
提问于 2015-11-01 15:42:34
回答 1查看 367关注 0票数 0

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

代码语言:javascript
复制
 //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
    }

}

}

我使用这个作为参考,尽管它是目标-C 实施

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-01 19:54:12

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

代码语言:javascript
复制
func childrenForItem (itemPassed : AnyObject?) -> Array<String>{
    if let item = itemPassed {
        let item = item as! String
        return outlineContents[item]!
    } else {
        return outlineTopHierarchy
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33463991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档