Ios Swift-UITableView与图像滞后
我的问题是当我向下滚动我的UITableView时,它看起来太滞后了。这些图片来自facebook 我的代码Ios Swift-UITableView与图像滞后,ios,uitableview,swift,xcode6,Ios,Uitableview,Swift,Xcode6,我的问题是当我向下滚动我的UITableView时,它看起来太滞后了。这些图片来自facebook 我的代码 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("UserCell", f
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UserCell", forIndexPath: indexPath) as UITableViewCell
let user = users[indexPath.row] as User //2
if let nameLabel = cell.viewWithTag(100) as? UILabel { //3
nameLabel.text = user.name
}
if let dateCreatedLabel = cell.viewWithTag(101) as? UILabel {
dateCreatedLabel.text = user.distance
}
if let profilePictureView = cell.viewWithTag(103) as? UIImageView {
if let url = NSURL(string: "https://graph.facebook.com/\(user.profilePhoto)/picture?type=large") {
if let data = NSData(contentsOfURL: url){
profilePictureView.contentMode = UIViewContentMode.ScaleAspectFit
profilePictureView.image = UIImage(data: data)
}
}
}
return cell
}
请建议如何使其平滑。天哪,不要这样做,不仅在滚动控件中,而且在一般UI中: data=NSData(contentsofull:url) 这就是为什么你的桌子落后了,而且你很幸运拥有了快速的互联网。如果你的连接速度慢,你的应用程序就会挂起,可能会永远挂起。始终异步进行网络连接 此外,当您使网络异步时,tableView仍将滞后于此: UIImage(数据:数据) 即使在这里,如果您的单元格中有许多控件: cell.viewWithTag(101) 所以,使用一些库来下载图像,这是一项令人惊讶的任务,并不像看上去那么容易,根据你的经验,你不会自己正确地完成它(正如我所看到的) 为您的单元制作单独的类,并使用IB连接插座
试试AFNetworking,它有UIImageView下载图像的类别。我已经找到了答案。使用
Haneke
代替NSData
import Haneke
/* .. */
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UserCell", forIndexPath: indexPath) as UITableViewCell
let user = users[indexPath.row] as User //2
if let nameLabel = cell.viewWithTag(100) as? UILabel { //3
nameLabel.text = user.name
}
if let dateCreatedLabel = cell.viewWithTag(101) as? UILabel {
dateCreatedLabel.text = user.distance
}
if let profilePictureView = cell.viewWithTag(103) as? UIImageView {
if let url = NSURL(string: "https://graph.facebook.com/\(user.profilePhoto)/picture?type=large") {
profilePictureView.contentMode = UIViewContentMode.ScaleAspectFit
profilePictureView.hnk_setImageFromURL(url!)
}
}
return cell
}
寻找有关在后台加载图像的问题和答案。那么我如何解决这个问题呢?@Dato'MohammadNurdin查看我的更新答案,你用swift编码,我想现在没有那么多库了(我用objective-c编码,有两个,试着采用AFNetworking f.e.)@Dato'MohammadNurdin是的,这是AFNetworking的作者,所以这正是你需要的。