イメージ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ライブラリをインストールし、以下の構文を使用してイメージを表示することです。これは、非同期ロードとすべてを処理します。 'セル?.thumbnailImage.af_setImage(withURL:<実際のサムネイル_URL>)' –