tableview 有时不加载,无论如何刷新它?

tableview is not loading sometimes , is there anyway to refresh it?

我是 swift 的新手,已经用 url 会话数据实现了一个表视图,它工作正常,除了图像没有加载,有时数据来得晚,表视图是空的。
我放了 http://localhost:3000/nameofimage.png ,我得到了我的图像但是在 tableview 中它不起作用

import UIKit
import Kingfisher

class BikelistViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
   

    var bikes =  [Bike]()
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bikes.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "mCellBike")
        let contentView = cell?.contentView
        let imageView = contentView?.viewWithTag(1) as! UIImageView
        let label = contentView?.viewWithTag(2) as! UILabel
        DispatchQueue.main.async {
            
            label.text = self.bikes[indexPath.row].model
            let url = URL(string: "http://localhost:3000/"+self.bikes[indexPath.row].image)
            imageView.kf.setImage(with: url)
       
        }
       
        return cell!
    }
   
    

    //passage de parametres entre les controleurs
        //cell OnclickListener
        
         func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let bike = bikes[indexPath.row]
            performSegue(withIdentifier: "mBikeDetails" , sender: bike) //passage de variable locale)
            
        }
        
        /* prepare est pour passer les parametres  */
        override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        
        if segue.identifier == "mBikeDetails" {
        
        let bike = sender as! Bike
        let destination = segue.destination as! BikeDetailsViewController
            destination.id = bike.bike_id
            destination.model = bike.model
            destination.type = bike.type
            destination.mprice = bike.price
            destination.image = bike.image
            
        
        }}
    


    override func viewDidLoad() {
        super.viewDidLoad()

        //get
      
       guard let url = URL(string: "http://localhost:3000/bikes") else {
       return
       }
       let session = URLSession.shared
       session.dataTask(with: url)  { ( data , response ,error) in
           if let response = response {
               print(response)
           }
           
           if let data = data {
               print(data)
               do
               {
                let json = try JSONSerialization.jsonObject(with: data, options: []) as! [[String:Any]]
                self.bikes.removeAll()
                
                for item in json {
                    let id = item["bike_id"] as! Int
                    let model = item["model"] as! String
                    let type = item["type"] as! String
                    let price = item["price"] as! String
                    let image = item["image"] as! String
                    self.bikes.append(Bike(id: id,model: model,type: type,price: price,image: image))
                }
                for item in self.bikes {
                    print(item.image)
                    print("http://localhost:3000/"+item.image)
                    
                }
                print(self.bikes)
               }catch{
                   print(error)
               }
            
           }
           
       }.resume()
       
        
        // Do any additional setup after loading the view.
    }
    


}
 

我正在尝试让我的图像加载到我的 tableview 中,我的数据正在显示,但我的图像除外。有时数据来晚了,tableview 是空的 我在这里遗漏了什么吗?

您正在尝试在主线程中获取数据,这就是它滞后的原因。只让主线程创建单元格对象,不要在这里实现任何网络操作,所以这在您的代码中是错误的:

imageView.image = UIImage(named: "http://localhost:3000/"+bikes[indexPath.row].image)

不应在主线程中执行 网络操作。如果您愿意,可以使用 KingFisher 等第三方库或直接使用您的资产文件夹。如果你这样做,你的 tableview 会很放松:

imageView.image = UIImage(named: "happy_bikeimage_coming_from_assets_folder")

例如取数据后台:

DispatchQueue.global(qos: .userInitiated).async {
    if let url = URL(string: urlString) {
        if let data = try? Data(contentsOf: url) {
            self.parse(json: data)
            return
        }
    }
}

如果在线加载图像,可以使用 kingFisher 检查:

或者您可以在您的资产文件夹中使用它并通过其名称正常调用图像:

DispatchQueue.main.async {
     UIImage(named: "imageName.extension")
}