Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我应该如何等待所有下载完毕的数据显示在TableView上?_Ios_Swift - Fatal编程技术网

Ios 我应该如何等待所有下载完毕的数据显示在TableView上?

Ios 我应该如何等待所有下载完毕的数据显示在TableView上?,ios,swift,Ios,Swift,我有一个名为ShowCarClass的类 它将使用TableView显示汽车的名称和图像 class ShowCarClass:UIViewController { @IBOutlet weak var tableView: UITableView! var car = [Car]() override func viewDidLoad() { super.viewDidLoad() } func myAPI() { startAnima

我有一个名为ShowCarClass的类

它将使用TableView显示汽车的名称和图像

class ShowCarClass:UIViewController {

@IBOutlet weak var tableView: UITableView!
var car = [Car]()

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

    func myAPI() {
         startAnimating()
         let json = CarType.Request
         api.getAPIResponse(apiURL:.Car ,jsonType: json) {(isSuccess, result) in

         switch isSuccess {

         case true: 
         let successResult = result as! CarType.Response
         //car array result like this
                 [Car(carName:"BMW",carImg:"https://00.00.00/static/image/BMW.png"),Car(carName:"Audi",carImg:"https://00.00.00/static/image/Audi.png")]
                 self.tableView.delegate = self
                 self.tableView.dataSource = self
                 self.tableView.reloadData()

         case false:
         self.APIErrorStatus(result)
                }
          stopAnimating()
           }
        }
当我按下按钮时,它可以调用myApi()并更新tableview内容

此ViewController有3个按钮,它将分别获取不同的数据

@IBAction func btnPress(_ sender: UIButton) {
         callAPI()
}
在cellForRowAt函数中,我使用carImg的URL下载图像

extension ShowCarClass:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return car.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CarCell
        cell.carNameLabel.text = car[indexPath.row].carName
        cell.carImageView.downloaded(from:car[indexPath.row].carImg, contentMode: .scaleToFill)
        return cell
    }
}
这是图像视图的扩展

extension UIImageView {
    func downloaded(from url: URL, contentMode mode: UIView.ContentMode = .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() {
                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)
    }
}
在ShowCarClass中,我在调用myAPI()时显示startAnimating

但我需要使用图像URL下载图像

这种情况将使开始和停止动画完成过快

TableView看起来我真的得到了我需要的数据

但车的数组内容有URL我必须另外处理

我希望它可以下载所有的图像,并实现停止动画

步骤如下:用户打开应用程序->调用myApi()->启动动画->所有

carName和CarImage完全->停止动画->加载到TableView

->用户可以查看所有车辆信息(在数据完成之前无法滑动tableview)


我是个新手,不擅长提问,如果需要更多信息,请直接问我,谢谢。

你可以使用DispatchGroup

let dispatchGroup = DispatchGroup()

let imageCount = 10

for _ in 1...imageCount{
   dispatchGroup.enter()
   ImageDownloader.download(image: "url") { result in
      dispatchGroup.leave()
   }
}

dispatchGroup.notify(queue: .main) 
    tableView.reloadData()
}

您可以使用DispatchGroup

let dispatchGroup = DispatchGroup()

let imageCount = 10

for _ in 1...imageCount{
   dispatchGroup.enter()
   ImageDownloader.download(image: "url") { result in
      dispatchGroup.leave()
   }
}

dispatchGroup.notify(queue: .main) 
    tableView.reloadData()
}

你能告诉我应该放在哪里吗?我需要修改UIImageView扩展名吗?这就是为什么它可以传递图像结果的原因?在result blok中的yor“myAPI”函数中,在self.tableView.reloadData()之后,但现在在您的单元格中,行方法不需要设置图像,只需显示活动指示器。当你下载所有的图片时,用现有的图片重新加载tableview,你能告诉我应该放在哪里吗?我需要修改UIImageView扩展名吗?这就是为什么它可以传递图像结果的原因?在result blok中的yor“myAPI”函数中,在self.tableView.reloadData()之后,但现在在您的单元格中,行方法不需要设置图像,只需显示活动指示器。下载所有图像时,请使用现有图像重新加载tableview