ios - Table Cells not loading on scroll -
i creating messaging page in app , having serious trouble. when page first loads looks fine when start scroll down gets messed up. new cells not load , when scroll cells gone except 1 shows , in wrong place.
there async action going on. when message sent sends server , when returns reload data , scroll newly entered message @ bottom. buggy well. of time when sending message new cell not show up. after sending 1 or 2 more show up.
here viewcontroller data source , delegate
import uikit class messagedetailcontroller: uiviewcontroller,uitableviewdatasource,uitableviewdelegate, uitextfielddelegate { @iboutlet weak var lblmessagetext: uitextfield! @iboutlet weak var sendviewheightconstraint: nslayoutconstraint! @iboutlet weak var messagestable: uitableview! @iboutlet weak var sendmsgview: uiview! @iboutlet weak var sendmessageview: uiview! var messagedetailarray = [message]() override func viewdidload() { super.viewdidload() self.lblmessagetext.delegate = self self.view.bringsubviewtofront(self.sendmessageview) self.messagestable.separatorstyle = uitableviewcellseparatorstyle.none self.messagestable.datasource = self self.messagestable.delegate = self self.messagestable.reloaddata() } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return messagedetailarray.count } func textfieldshouldreturn(textfield: uitextfield) -> bool { _ = messageservice().sendmessage("james", message: lblmessagetext.text!, callback: loadmessages) lblmessagetext.text = "" return true } func loadmessages(jsondata:nsarray){ dispatch_async(dispatch_get_main_queue(),{ var messages = [message]() messagetest in jsondata { if let message = messagetest as? nsdictionary { messages.append(message(messagetype:message.qa.a, from:message["user_display_label"] as! string, messagetext:message["message"] as! string, timesent:nsdate())) } } self.messagedetailarray = messages; self.messagestable.reloaddata() self.messagestable.scrolltorowatindexpath(nsindexpath(forrow: self.messagedetailarray.count - 1, insection: 0), atscrollposition: uitableviewscrollposition.bottom, animated: true) }) } @ibaction func endedediting(sender: anyobject) { self.view.layoutifneeded() uiview.animatewithduration(0.2, animations: { self.sendviewheightconstraint.constant = 40 self.view.layoutifneeded() }, completion: nil) } @ibaction func startedediting(sender: anyobject) { self.view.layoutifneeded() uiview.animatewithduration(0.2, animations: { self.sendviewheightconstraint.constant = 295 self.view.layoutifneeded() }, completion: nil) } @ibaction func sendmessage(sender: anyobject) { _ = messageservice().sendmessage("james", message: lblmessagetext.text!, callback: loadmessages) lblmessagetext.text = "" } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let thismsg = messagedetailarray[indexpath.row] let cell: messagedetailatableviewcell = tableview.dequeuereusablecellwithidentifier("messagecentera", forindexpath: indexpath) as! messagedetailatableviewcell cell.msglabel?.text = thismsg.messagetext; cell.msglabel.sizetofit() cell.msglabel.numberoflines = 0 cell.frame = cgrectmake(0, 0, self.view.bounds.width, cell.frame.size.height); let myinsets = uiedgeinsetsmake(7, 8, 7, 8) let bubbleimage = uiimage(named: "balloon_read_right")?.resizableimagewithcapinsets(myinsets) let bubbleimageview = uiimageview(image: bubbleimage) bubbleimageview.frame = cgrectmake(0, 0, cell.bubbleview.bounds.width, cell.bubbleview.bounds.height) cell.bubbleview.addsubview(bubbleimageview) cell.bubbleview.bringsubviewtofront(cell.msglabel) return cell } func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat { let messagesize:cgsize = ptsmessagingcell.messagesize(messagedetailarray[indexpath.row].messagetext) return messagesize.height + 2 * ptsmessagingcell.textmarginvertical() + 40 } }
if read through notice use class include you. tablecell , not much.
import uikit class messagedetailatableviewcell: uitableviewcell { @iboutlet weak var bubbleview: uiview! @iboutlet weak var msgimageview: uiimageview! @iboutlet weak var msglabel: uilabel! override func awakefromnib() { super.awakefromnib() // initialization code } override func setselected(selected: bool, animated: bool) { super.setselected(selected, animated: animated) // configure view selected state } }
i have messed around code lot trying work , cont find else has same bug. appreciated!
thanks!!
the problem caused fact set frame
of cells. set origin of cells (0,0). destroys layout of table view. should never touch frames of cells outside.
if want set height of cells have in uitableviewdelegate
method tableview(_:heightforrowatindexpath:)
. calculate cell height there , return it.
you not have set width of cells, because in uitableview
uitableviewcells
have same width uitableview
.
Comments
Post a Comment