ios - Parse and Swift. Set tableViewCell accessory type from Parse relation -
so, have got tableview shows courses. user able set checkmarks on these courses (cells) , save them in pfuser object relation courses class (where courses stored). question is, how checkmark courses user has saved @ point before.
this attempt, don’t know how continue. how cells specific label? (or there better way?)
let courserel = pfuser.currentuser()?.relationforkey("usercourses") let query = courserel!.query() let qobjects :array = query!.findobjects()! println(qobjects) var qobjectscount = qobjects.count; qobjectscount > 0; --qobjectscount { var qanobject: anyobject = qobjects[qobjectscount - 1] var coursename = qanobject["coursename"] println(coursename) if let cell: anyobject? = tableview.dequeuereusablecellwithidentifier("coursecell"){ } }
edit: code in override viewdidload
edit2:
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath, object: pfobject?) -> pftableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("coursecell") as! pftableviewcell! if cell == nil { cell = pftableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "coursecell") } let courserel = pfuser.currentuser()?.relationforkey("usercourses") let query = courserel!.query() let qobjects :array = query!.findobjects()! // extract values pfobject display in table cell if let coursename = object?["coursename"] as? string { cell?.textlabel?.text = coursename if contains(qobjects, object) { cell?.accessorytype = uitableviewcellaccessorytype.checkmark } } return cell }
error in line ‚if contains(qobjects, object) {'
generic parameter 's.generator.element’ cannot bound non-@objc protocol type 'anyobject'
edit3:
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath, object: pfobject?) -> pftableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("coursecell") as! pftableviewcell! if cell == nil { cell = pftableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "coursecell") } let courserel = pfuser.currentuser()?.relationforkey("usercourses") let query = courserel!.query() let qobjects :array = query!.findobjects()! // extract values pfobject display in table cell if let coursename = object?["coursename"] as? string { cell?.textlabel?.text = coursename cell.tintcolor = uicolor.blackcolor() } if contains(qobjects, { $0 === object }) { cell?.accessorytype = uitableviewcellaccessorytype.checkmark self.selectedrows.addindex(indexpath.row) }else{ cell?.accessorytype = uitableviewcellaccessorytype.none } return cell }
edit4: (working code)
in class:
// initializing qobject variable var qobjects :array<anyobject> = []
in objectsdidload:
// pfobjects checkmarks on courses currentuser has selected before let courserel = pfuser.currentuser()?.relationforkey("usercourses") let query = courserel!.query() qobjects = query!.findobjects()!
in tableview(cellforrowatindexpath):
// extract values pfobject display in table cell if contains(qobjects, { $0 === object }) { cell.accessorytype = uitableviewcellaccessorytype.checkmark self.selectedrows.addindex(indexpath.row) } else { cell.accessorytype = uitableviewcellaccessorytype.none }
don't try search cell. well, can, not you're trying - i'll come that.
your current code creating new cells, not finding existing cells, won't work.
what should doing storing array of returned objects, qobjects
, , when you're configuring cell display checking if array contains object current cell. if does, tick it, otherwise remove tick.
now, if load of qobjects
happens after view shown have 2 options:
- reload table view
- update visible items
option 2. better, if user might scrolling list. want use array returned calling indexpathsforvisiblerows
on table view. then, iterate list, associated object , check if it's in qobjects
, cell on display cellforrowatindexpath:
, update it.
Comments
Post a Comment