こちらの動画をみながらStoryboardでテーブルセルにUIimageView
を設定しURL先の画像を表示したいのですがうまくいきません。
画像をUIimageView
に表示する場合はHttpRequest
を使用するのでしょうか。
ご教授いただけると幸いです。
TableViewController
1import UIKit 2 3class StoreListViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet weak var TableView: UITableView! 6 7 let cellReuseIdentifier = "cell" 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 TableView.delegate = self 12 TableView.dataSource = self 13 } 14 15 override func viewDidAppear(_ animated: Bool) { 16 super.viewDidAppear(animated) 17 18 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 24 } 25 26 27 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 28 return stores?.count ?? 0 29 } 30 31 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 32 guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell 33 else { 34 fatalError("The dequeued cell is not an instance of TableViewCell.") 35 } 36 37 if let name:String = stores![indexPath.row].name{ 38 cell.storeName.text! = name 39 }else{ 40 cell.storeName!.text = "nil" 41 } 42 if let location : String = stores![indexPath.row].location{ 43 cell.storeLocation.text! = location 44 }else{ 45 cell.storeLocation.text! = "nil" 46 } 47 if let imageURL = URL(string: "http://127.0.0.1:8000/photos/cake_0.jpeg"){ 48 DispatchQueue.global().async { 49 let data = try? Data(contentsOf: imageURL) 50 if let data = data { 51 let image = UIImage(data: data) 52 DispatchQueue.main.async { 53 cell.imageCell.image = image 54 } 55 } 56 } 57 } 58 return cell 59 } 60} 61
TableViewCell
1import UIKit 2 3class TableViewCell: UITableViewCell { 4 5 6 @IBOutlet weak var imageCell: UIImageView! 7 @IBOutlet weak var storeName: UILabel! 8 @IBOutlet weak var storeLocation: UILabel! 9 10 11 override func awakeFromNib() { 12 super.awakeFromNib() 13 // Initialization code 14 } 15 16 override func setSelected(_ selected: Bool, animated: Bool) { 17 super.setSelected(selected, animated: animated) 18 19 // Configure the view for the selected state 20 } 21 22}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。