【发布时间】:2017-06-20 00:36:43
【问题描述】:
我是 Swift 新手,我正在使用 ObjectMapper 解析我的 JSON,但我希望数据显示在 TableView 中。但是我有下载图像的问题 我使用扩展 UIImageView func downloadFrom
我的问题:
'download From(url:ContentMode:)'的重新声明无效
我的代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryTableViewCell", for: indexPath) as! CategoryTableViewCell
let strUrl = categoty[indexPath.row].picture
cell.titleCategory.text = self.categoty[indexPath.row].title
cell.imageCategory.downloadFrom(url: URL(string: strUrl!)!)
return cell
}
}
extension UIImageView {
func downloadFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { () -> Void in
self.image = image
}
}.resume()
}
}
【问题讨论】:
-
这本质上意味着你已经声明了两个同名的函数..只需重命名你的函数