无法从API获取图像以在Swift的UIImageView中加载

时间:2018-06-25 18:34:13

标签: ios swift

我正在尝试从API中获取100张图片,以便在表格视图中查看。我正在使用以下代码将图像放入单元格:

import os
 os.system("./etcdctl cluster-health")

}


我遇到以下错误:

1)方法不会覆盖其超类中的任何方法 2)类型import UIKit class CatImageTableViewCell: UITableViewCell { //URL containing the image let URL_IMAGE = URL(string: "http://example.com/api/images/get?format=xml&results_per_page=100") @IBOutlet weak var catImage: UIImageView! override func viewDidLoad() { super.viewDidLoad() let session = URLSession(configuration: .default) //creating a dataTask let getImageFromUrl = session.dataTask(with: URL_IMAGE!) { (data, response, error) in //if there is any error if let e = error { //displaying the message print("Error Occurred: \(e)") } else { //in case of now error, checking wheather the response is nil or not if (response as? HTTPURLResponse) != nil { //checking if the response contains an image if let imageData = data { //getting the image let image = UIImage(data: imageData) //displaying the image self.catImage.image = image } else { print("Image file is currupted") } } else { print("No response from server") } } } //starting the download task getImageFromUrl.resume() } 的值没有成员UITableViewCell


如果您需要更多代码,请告诉我您需要什么,我会予以张贴。

3 个答案:

答案 0 :(得分:0)

  

1)方法不覆盖其超类中的任何方法2)的值   类型“ UITableViewCell”没有成员“ viewDidLoad”

1)此错误告诉您,父类的'override'字词'overrides'方法。

2)UITableViewCell不是UIViewController的子类,viewDidLoad是UIViewController的方法。

答案 1 :(得分:0)

1)方法不会覆盖其超类中的任何方法2)类型为'UITableViewCell'的值没有成员'viewDidLoad'

此行是因为您在UITableViewCell类中调用了viewDidLoad方法,因此删除

override func viewDidLoad() { super.viewDidLoad() }

在UITableViewCell类中并在UIViewController类中编写

答案 2 :(得分:0)

使用 awakeFromNib

更改 viewDidLoad