ios - Annoying TableView while using Twitter API -
hello making application using twitter api. have tablevviewcontroller, loads followers , people follow (friends). then, tableviewcontroller checks if friends our followers. if user user in friends array doesn't follow us, sent 'results' array , tableview loads data.but when scroll tableview, annoying things happens. here code , video link; click go video.
class tableviewcontroller: uitableviewcontroller { var results = nsmutablearray() var following = nsarray() var followers = nsarray() override func viewdidload() { super.viewdidload() tableview.delegate = self tableview.reloaddata() twitter.sharedinstance().apiclient.sendtwitterrequest(twitter.sharedinstance().apiclient.urlrequestwithmethod("get", url: "https://api.twitter.com/1.1/friends/ids.json", parameters: ["user_id":twitter.sharedinstance().session().userid], error: nil), completion: {(response:nsurlresponse?, data:nsdata?, error:nserror?) -> void in var dic = nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.allowfragments, error: nil) as! nsdictionary var friends = dic["ids"] as! nsarray self.following = friends twitter.sharedinstance().apiclient.sendtwitterrequest(twitter.sharedinstance().apiclient.urlrequestwithmethod("get", url: "https://api.twitter.com/1.1/followers/ids.json", parameters: ["user_id":twitter.sharedinstance().session().userid], error: nil), completion: {(resonse:nsurlresponse?,data:nsdata?,error:nserror?) -> void in var dic = nsjsonserialization.jsonobjectwithdata(data!, options: nsjsonreadingoptions.allowfragments, error: nil) as! nsdictionary var followers = dic["ids"] as! nsarray self.followers = followers id in self.following { if !self.followers.containsobject(id) { var st = id as! nsnumber twitter.sharedinstance().apiclient.loaduserwithid(st.stringvalue, completion: {(user:twtruser?,error:nserror?) -> void in self.results.addobject(user!) self.tableview.reloaddata() println("done") }) } } }) })} override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { if indexpath.row == 0 { var cell = tableview.dequeuereusablecellwithidentifier("cellid", forindexpath: indexpath) as! uitableviewcell cell.textlabel?.text = "you have \(results.count) bad friends." return cell }else { var cell = tableview.dequeuereusablecellwithidentifier("cellid", forindexpath: indexpath) as! customcell var user = results.objectatindex(indexpath.row-1) as! twtruser cell.user = user cell.label.text = user.name cell.imageframe.image = uiimage(data: nsdata(contentsofurl: nsurl(string: user.profileimagelargeurl)!)!) return cell }
as table view scrolls, cells re-used - object returned dequeuereusablecellwithidentifier
may new cell or may object has been used - in case old content still there.
if have custom uitableviewcell
subclass can implement prepareforreuse
method in class clear old content or need set things in cellforrowatindexpath
-
override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { var cell = tableview.dequeuereusablecellwithidentifier("cellid", forindexpath: indexpath) as! customcell if indexpath.row == 0 { cell.textlabel?.text = "you have \(results.count) bad friends." cell.user = nil cell.label.text="" cell.imageframe.image=nil return cell } else { cell.textlabel?.text="" cell.user = user cell.label.text = user.name cell.imageframe.image = uiimage(data: nsdata(contentsofurl: nsurl(string: user.profileimagelargeurl)!)!) return cell } return cell }
Comments
Post a Comment