我有圖像的URL,我想在放置在tableView單元格中的UIImageView中顯示該圖像。 我創建了一個自定義單元格,併爲imageView添加了一個插口。 由於我正在加載新聞,因此URL發生相應變化。 注意:我正在使用Alamofire來處理我的HTTP請求。如何從URL加載圖像並將其顯示在tableView單元格中(Swift 3)
struct News {
let title: String
let text: String
let link: String
let imgUrl: String
init(dictionary: [String:String]) {
self.title = dictionary["title"] ?? ""
self.text = dictionary["text"] ?? ""
self.link = dictionary["link"] ?? ""
self.imgUrl = dictionary["imgUrl"] ?? ""
}
}
和加載信息到我的自定義單元格
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? newsCellTableViewCell
let news = newsData[indexPath.row]
cell?.headline.text = news.title
cell?.excerpt.text = news.text
cell?.thumbnailImage.text = news.imgUrl
return cell!
}
可能重複http://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift/27712427#27712427 –
@LeoDabus提出好的帖子。但由於您已經在使用Alamofire,最簡單的方法是安裝https://github.com/Alamofire/AlamofireImage庫並使用以下語法顯示圖像。這負責異步加載和所有。 'cell?.thumbnailImage.af_setImage(withURL:)' –