swift - NSOutlineView, using item: AnyObject -
i'm creating nsoutlineview. when implementing data source, although i'm able create top hierarchy can not implement childhierarchy. reason can't read item: anyobject? prevents me returning right array dictionary.
//mark: nsoutlineview var outlinetophierarchy = ["collect", "review", "projects", "areas"] var outlinecontents = ["collect":["a","b"], "review":["c","d"],"projects":["e","f"],"areas":["g","h"]] //get children item func childrenforitem (itempassed : anyobject?) -> array<string>{ var childrenresult = array<string>() if(itempassed == nil){ //if no item passed return highest level of hirarchy childrenresult = outlinetophierarchy }else{ //issue here: //need find title call correct child childrenresult = outlinecontents["collect"]! //faked, should showing top hierarchy item return 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? { // groups, return 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{ // cell setup in ib. textfield , imageview outlets setup. let resulttextfield = outlineview.makeviewwithidentifier("datacell", owner: self) as! nstablecellview resulttextfield.textfield!.stringvalue = item as! string return resulttextfield } }
}
i used reference, although it's objective-c implemented
you need cast item
correct type outline. you'd want use real data model, toy problem 2 levels in hierarchy, suffices:
func childrenforitem (itempassed : anyobject?) -> array<string>{ if let item = itempassed { let item = item as! string return outlinecontents[item]! } else { return outlinetophierarchy } }
Comments
Post a Comment