Ios 无法从API获取要在Swift的UIImageView中加载的图像
我试图从一个API中获取100个可以在表视图中查看的图像。我正在使用以下代码将图像放入单元格:Ios 无法从API获取要在Swift的UIImageView中加载的图像,ios,swift,Ios,Swift,我试图从一个API中获取100个可以在表视图中查看的图像。我正在使用以下代码将图像放入单元格: 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 we
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()
}
}
我收到以下错误: 1) 方法不重写其超类中的任何方法 2) 类型
UITableViewCell
的值没有成员viewDidLoad
如果你需要更多的代码,让我知道你需要什么,我会发布它 1) 方法不会从其超类2)值重写任何方法 类型“UITableViewCell”没有成员“viewDidLoad” 1) 此错误告诉您,“override”单词“overrides”方法来自父类 2) UITableViewCell不是UIViewController中的子类,viewDidLoad是UIViewController中的方法。1)方法不重写其超类中的任何方法2)类型为“UITableViewCell”的值没有成员“viewDidLoad” 因为您在UITableViewCell类中调用viewDidLoad方法,所以请删除该行
override func viewDidLoad(){
super.viewDidLoad()
}
在UITableViewCell类中,并在UIViewController类中写入更改viewDidLoad,使用awakeFromNib为什么要在UITableViewCell子类中获取图像?最好在助手类或UIViewController上编写此代码。实际上,此代码中存在许多问题。我建议使用这个图书馆