ios - Two prototype cells in one section -
i have 2 prototype cells. 1 appears if messagesarray[indexpath.row]
value "", other if value contains characters. 1 of cells' row height greater second , contains additional variables. they're both hooked own cell classes , have own cell identifiers. want them both coexist in same tableview, under 1 section, i'm struggling achieve that. keep getting fatal error: array index out of range
. array value being populated async db request, explanation.
what doing wrong/how can successfully?
var messagesarray = [string]() override func viewdidload() { super.viewdidload() var query = pfquery(classname: "class") query.findobjectsinbackgroundwithblock { (objects, error) -> void in if error == nil { if let objects = objects { object in objects { if let message = object["message"] as? string { self.messagesarray.append(message) } } } } else { println(error) } } } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { if self.messagesarray[indexpath.row] == "" { var cell = tableview.dequeuereusablecellwithidentifier("cellone", forindexpath: indexpath) as! cellone return cell } else { var cell = tableview.dequeuereusablecellwithidentifier("celltwo", forindexpath: indexpath) as! celltwo return cell } }
edit: if messagesarray[indexpath.row] ==
value other ""
(there's message), first cell in message displayed in larger second cell , displayed uilabel
doesn't exist in second cell.
if using dynamic cells must not have 2 prototype cells on tableview, 1 needed job done.
var messagesarray = [string]() override func viewdidload() { super.viewdidload() var query = pfquery(classname: "class") query.findobjectsinbackgroundwithblock { (objects:[anyobject]?, error:nserror) -> void in if error == nil { if let objects = objects as? [pfobject] { object in objects { var message = object["message"] self.messagesarray.append(message) self.tableview.reloaddata() } } } else { println(error) } } } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("cellone", forindexpath: indexpath) as! cellone var data = self.messagesarray[indexpath.row] cell.textlabel.text = data
}
Comments
Post a Comment