ios - tableView jumping scrolling after reloadData -
my tableview
scrolling jumping when reload tableview
new data. here code cellforrowatindexpath
:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let section = indexpath.section let index = indexpath.row let info = data[sections[section]]![index] conversationmessage if info.from == .me { var cell: conversationdialogmetablecell = tableview.dequeuereusablecellwithidentifier("mecell", forindexpath: indexpath) as! conversationdialogmetablecell cell.backgroundcolor = uicolor.clearcolor() cell.selectionstyle = .none cell.messagelabel.text = info.text return cell } else if info.from == .other { var cell: conversationdialogothertablecell = tableview.dequeuereusablecellwithidentifier("othercell", forindexpath: indexpath) as! conversationdialogothertablecell cell.backgroundcolor = uicolor.clearcolor() cell.selectionstyle = .none cell.personname.textcolor = uicolor(hex: 0x6d6d6d) cell.messagecontainerview.backgroundcolor = uicolor(hex: 0x6d6d6d) cell.messagecontainerview.layer.cornerradius = 5 cell.messagecontainerview.clipstobounds = true alamofire.request(.get, info.personimage).response { (request, response, data, error) in let image = uiimage(data: data!, scale: 1)! cell.personimage.image = image cell.personimage.contentmode = uiviewcontentmode.scaleaspectfill cell.personimage.layer.cornerradius = cell.personimage.frame.height / 2 cell.personimage.clipstobounds = true } cell.personname.text = info.personname cell.personmessage.text = info.text return cell } return uitableviewcell() }
for first load scrolling smooth, if add new data in tableview
, call reloaddata()
scrolling jumping when new cells shown.
here code insert new data model:
func client(client: pubnub!, didreceivemessage message: pnmessageresult!) { if message.data.subscribedchannel == self.channel { if let messageinfo: anyobject = message.data.message { let date = (messageinfo["date"] as! string).getdatestring() let messagetext = messageinfo["message"] as! string let from: conversationmessage.sendfromtype = (messageinfo["userid"] as! string) == self.userid ? .me : .other let image = messageinfo["userphoto"] as! string let name = messageinfo["username"] as! string if data[date] != nil { data[date]!.append(conversationmessage(text: messagetext, from: from, personimage: image, personname: name, date: messageinfo["date"] as! string)) } else { data[date] = [conversationmessage(text: messagetext, from: from, personimage: image, personname: name, date: messageinfo["date"] as! string)] } section in self.sections { self.data[section]! = sorted(self.data[section]!) { utils.comparedatetime($0.date, with: $1.date, order: .orderedascending) } } tableview.reloaddata() tableviewscrolltobottom(false) } } }
maybe happens because of function scroll tableview
bottom:
func tableviewscrolltobottom(animated: bool) { let delay = 0.1 * double(nsec_per_sec) let time = dispatch_time(dispatch_time_now, int64(delay)) dispatch_after(time, dispatch_get_main_queue(), { let numberofsections = self.tableview.numberofsections() let numberofrows = self.tableview.numberofrowsinsection(numberofsections - 1) if numberofrows > 0 { let indexpath = nsindexpath(forrow: numberofrows - 1, insection: (numberofsections - 1)) self.tableview.scrolltorowatindexpath(indexpath, atscrollposition: uitableviewscrollposition.bottom, animated: animated) } }) }
one more thing. should cache uploaded images? maybe causing jumping scroll. can me solve issue?
Comments
Post a Comment