gpt4 book ai didi

ios - Swift - UITableView 滞后于图像

转载 作者:可可西里 更新时间:2023-11-01 00:16:54 26 4
gpt4 key购买 nike

我的问题是当我向下滚动我的 UITableView 时,它看起来太慢了。图片来自 facebook。

我的代码

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
}

请指教如何让它顺利。

最佳答案

OMG,不仅在滚动控件中,而且在一般 UI 中也不要这样做:

data = NSData(contentsOfURL: url)

这就是为什么你的表滞后,而你幸运地拥有快速的互联网。如果你的连接速度很慢,你的应用程序就会挂起,可能永远挂起。总是异步地做网络!

此外,当您使网络异步时,您的 tableView 仍会滞后于此:

UIImage(data: data)

即使在这里,如果您的单元格中有很多控件:

cell.viewWithTag(101)

所以,使用一些库来下载图像,这并不是看起来那么容易的任务,令人惊讶的是,根据你的经验,你不会自己做对的(正如我所看到的)。

为您的单元创建单独的类并使用 IB 连接 socket 。

尝试 AFNetworking,它有用于 UIImageView 下载图像的类别。

关于ios - Swift - UITableView 滞后于图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28380613/

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