【发布时间】:2017-06-08 02:08:55
【问题描述】:
我对如何获取作为链接的 DownloadURL 并将其转换为图像并将其添加到我的 tableview 单元格中感到困惑?我的故事板中有一个图像视图,也不知道如何将它连接到表格视图单元格。谢谢!
override func viewDidLoad() { super.viewDidLoad()
let ref = FIRDatabase.database().reference().child("Posts")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.childrenCount)
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let value = rest.value as? Dictionary<String,Any> else { continue }
guard let downloadURL = value["DownloadURL"] as? String else { continue }
let post = postStruct(title: title, date: date, author: author, article: article, downloadURL: downloadURL)
self.posts.append(post)
}
self.posts = self.posts.reversed(); self.tableView.reloadData()
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title
return cell!
}
【问题讨论】:
-
为你的 cell 定义一个类,并添加一个 UIImageView 作为 outlet,然后在 cellForRowAtIndexpath 中使用像 Alamofire 或 SDWebImage 这样的图像加载器来加载你的图像
-
如果我的回答有帮助,请标记它(按寒鸦)让其他人知道它是正确的
标签: ios swift uitableview url uiimageview