ios - 下载每个图像后重新加载 tableview

标签 ios swift uitableview alamofire

我做了以下代码并且工作得相当不错,但是我找不到在下载每个图像后重新加载 tableView 的方法。我正在使用 Alamofire 下载所有内容列表数据和图像。

如何从扩展访问 tableView?

如果有人能给我任何建议,我将不胜感激:)

import UIKit
import Alamofire
import AlamofireImage

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var jsonArray: NSArray?
    var nameArray: Array<String> = []
    var imageURLArray: Array<String> = []

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

    func downloadDataFromAPI(){
        Alamofire.request("http://private-135b4e-solamente.apiary-mock.com/pokemonList") .responseJSON { response in
            if let JSON = response.result.value{
                self.jsonArray = JSON as? NSArray
                for item in self.jsonArray! as! [NSDictionary]{
                    //5.
                    let name = item["name"] as? String
                    let imageURL = item["image"] as? String
                    self.nameArray.append((name)!)
                    self.imageURLArray.append((imageURL)!)
                }
                print(self.nameArray)
                self.tableView.reloadData()
            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.nameArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell:UITableViewCell=UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "mycell")
        cell.textLabel?.text  = nameArray[indexPath.row]
        cell.imageView!.downloaded(from: imageURLArray[indexPath.row])

        return cell
    }

}

extension UIImageView {
    func downloaded(from url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) {  // for swift 4.2 syntax just use ===> mode: UIView.ContentMode
        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() {
                self.image = image
            }
            }.resume()
    }
    func downloaded(from link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
        guard let url = URL(string: link) else { return }
        downloaded(from: url, contentMode: mode)
    }
}

最佳答案

您无需在下载每张图片后重新加载表格 View 。您需要在此函数的回调中返回图像 func downloaded 您可以在 objective-c 中找到类似的答案。

Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling

关于ios - 下载每个图像后重新加载 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57260101/

相关文章:

uitableview - 如何在 SwiftUI 中根据内容大小设置 tableview 高度?

ios - 将功能绑定(bind)到键盘中的 "Search"按钮

ios - 从 Cognito 向客户端发布身份验证自定义响应

ios - 未调用 Swift didReceiveRemoteNotification

ios - Firebase 数据库在删除节点时不断重新插入节点

ios - 本地通知限制

iphone - 我想在 UIAlertView 上显示 UIProgressView

ios - 无法通过 IOS 套接字发送消息

ios - UITableView 自定义单元格选项无法修改源变量

ios - 我如何使用 uitableview 单元格来执行 segue