【发布时间】:2017-03-24 11:17:33
【问题描述】:
此代码有问题。基本上,我正在尝试使用从 Twitter 中提取的图像填充表格单元格。这里的 url 字段的值为 http://pbs.twimg.com/profile_images/796924570150301696/35nSG5nN_normal.jpg 但由于某种原因 print("REACHED") 从未打印。任何帮助/建议表示赞赏!
代码sn-p:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tIdentifier, for: indexPath) as! TweetCell
let tweet = tweets[indexPath.section][indexPath.row]
let url = tweet.user.profileImageURL!
print(url.absoluteString)
let data = try? Data(contentsOf: url)
if (data == nil){
} else {
print("REACHED")
cell.avatarImage = UIImage(data: data!)
}
cell.tweet = tweets[indexPath.section][indexPath.row]
return cell
}
【问题讨论】:
-
let data = try? Data(contentsOf: url)避免这种情况!它同步并阻塞了 UI。 -
@Larme 对替代方法有什么建议吗?
标签: ios swift uitableview uiimage tableview