gpt4 book ai didi

ios - 使用 Twitter API 时烦人的 TableView

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:20 28 4
gpt4 key购买 nike

您好,我正在使用 twitter api 制作一个应用程序。我有一个 tablevViewController,它加载关注者和我们关注的人( friend )。然后,这个 tableViewController 检查 friend 是否是我们的追随者。如果 friend 数组中的用户不关注我们,它将被发送到“结果”数组并且 tableview 加载该数据。但是当我滚动 tableview 时,会发生一些烦人的事情。这是我的代码和视频链接; Click to go to 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
for 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
}

最佳答案

随着表格 View 的滚动,单元格将被重新使用 - 从 dequeueReusableCellWithIdentifier 返回的对象可能是一个新单元格,也可能是以前使用过的对象 - 在这种情况下是旧的内容仍然存在。

如果您有自定义的 UITableViewCell 子类,那么您可以在该类中实现 prepareForReuse 方法来清除旧内容,或者您​​需要在 中进行设置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
}

关于ios - 使用 Twitter API 时烦人的 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31403591/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com