Ios 使用Twitter API时的恼人的TableView
您好,我正在使用twitter api制作一个应用程序。我有一个tablevViewController,它加载追随者和我们关注的人(朋友)。然后,这个tableViewController检查朋友是否是我们的追随者。如果朋友数组中的user as user不跟随我们,它将被发送到“results”数组,tableview将加载该数据。但是当我滚动tableview时,会发生一些恼人的事情。这是我的代码和视频链接;Ios 使用Twitter API时的恼人的TableView,ios,swift,uitableview,twitter,Ios,Swift,Uitableview,Twitter,您好,我正在使用twitter api制作一个应用程序。我有一个tablevViewController,它加载追随者和我们关注的人(朋友)。然后,这个tableViewController检查朋友是否是我们的追随者。如果朋友数组中的user as user不跟随我们,它将被发送到“results”数组,tableview将加载该数据。但是当我滚动tableview时,会发生一些恼人的事情。这是我的代码和视频链接; 当您的表视图滚动时,单元格将被重新使用-从dequeueReusableCel
当您的表视图滚动时,单元格将被重新使用-从
dequeueReusableCellWithIdentifier
返回的对象可能是新的单元格,也可能是以前使用过的对象-在这种情况下,旧内容仍将存在
如果您有一个自定义的UITableViewCell
子类,那么您可以在该类中实现prepareforeuse
方法来清除旧内容,或者您需要在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
}
当您在cellforrowatinexpath
中重用单元格时,需要重置单元格状态。目前您没有清除if中的用户/图像等或else中的文本标签。@Paulw11您能给我一个示例代码吗?
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
}