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



我正在尝试从 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 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( 没有成员viewDidLoadUITableViewCell类型的值


如果您需要更多代码,请告诉我您需要什么,我会发布它。

1( 方法

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

1(此错误告诉您,父类中的"覆盖"一词"覆盖"方法。

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

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

这一行因为你在UITableViewCell类中调用了viewDidLoad方法,所以删除

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

在 UITableViewCell 类中并写入 UIViewController 类

changeviewDidLoadwithawakeFromNib

最新更新