ios - Swift/Parse: Elements in table view cell with nil value displaying values from other cells -
my project has table view (specifically pfquerytableview i'm using parse backend), displays either text or picture (in addition standard elements poster's profile picture , name). think of facebook-like feed posts can either contain text or image. means in "yell" class (posts), either "yelltext" (text in post) or "picturefromcamera" (picture in post) value nil. these loaded custom table view cell (prototype cell) containing label text, , pfimageview images. allows me change formatting of cell using if/else statements depending on whether or not "posttext" or "postimage" given cell nil. works when open view controller, , looks this:
when use pull refresh function, images placed in cells in not meant be:
and object looks in parse:
my code container view containing pfquerytableview follows:
class yellcontenttableviewcontroller: pfquerytableviewcontroller { var posterfirstname: string! var hasupvoted: bool? // initialise pfquerytable tableview override init(style: uitableviewstyle, classname: string!) { super.init(style: style, classname: classname) } required init(coder adecoder: nscoder) { super.init(coder: adecoder) // configure pfquerytableview self.parseclassname = "yell" self.textkey = "yelltext" self.pulltorefreshenabled = true self.paginationenabled = true if (self.paginationenabled == true){ self.objectsperpage = 20 } } // define query provide data table view override func queryfortable() -> pfquery { var query = pfquery(classname: "yell") query.orderbydescending("createdat") return query } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath, object: pfobject?) -> pftableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("customcell") as! customtableviewcell! if cell == nil { cell = customtableviewcell(style: uitableviewcellstyle.default, reuseidentifier: "customcell") } if let posterfirstname = object?["posterfirstname"] as? string { cell.customposterfirstname.text = posterfirstname } //display poster's profile picture var initialthumbnail = uiimage(named: "temporary") cell.customposterprofilepicture.image = initialthumbnail if let thumbnail = object?["posterprofilepicture"] as? pffile { cell.customposterprofilepicture.file = thumbnail cell.customposterprofilepicture.loadinbackground() } if let score = object?.valueforkey("votecount") as? int { cell.yellvotes.text = "\(score)" } let yelltext = object?["yelltext"] as? string if (yelltext == "a"){ cell.customyelltext.text = "" var picturefromcamera = object?["picturefromcamera"] as? pffile tableview.rowheight = 90 cell.custompicturefromcamera.file = picturefromcamera cell.custompicturefromcamera.loadinbackground() } if (yelltext != "a"){ tableview.rowheight = 60 if let yelltext = object?["yelltext"] as? string { cell.customyelltext.text = yelltext cell.custompicturefromcamera.file = nil } } return cell }
and prototype cell:
class customtableviewcell: pftableviewcell { @iboutlet weak var custompicturefromcamera: pfimageview! @iboutlet weak var customyelltext: uilabel! @iboutlet weak var customposterfirstname: uilabel! @iboutlet weak var customposterprofilepicture: pfimageview!
these outlets connected imageviews , labels in prototype cell in storyboard.
as may notice, determinant of whether or not customtableviewcell formatted being picture cell or text cell if/else statement regarding whether or not yelltext == a. @ first based on if/else statement regarding if custompicturefromcamera == nil. when did however, yelltexts other cells entered picture cells (where yelltext element in parse object undefined/nil). quick , dirty solution set yelltext "a" picture posts ("yells"), set if/else statement depend of if yelltext == "a", , if yes, set yelltext = "" no text shows in picture cells. works, not elegant solution. considered similar approach picture issue (uploading small file picturefromcamera element in parse object no longer set undefined/nil), terribly inefficient, i'm hoping find better solution.
so, have idea why refreshing pfquerytableview leads nil values given cell being replaced values other cells?
one way solve problem implement func prepareforreuse()
in table cell class such resets elements of cell reasonable values. way, can sure each cell cleared out before it's reused, old values won't show up.
another option ensure set every element of cell in tableview(_ tableview:,cellforrowatindexpath:)
method. reasonable if you're not using custom cell class, example.
Comments
Post a Comment