简体   繁体   中英

How to use SDWebImage to show images in tableview Swift?

I am trying to grab the image url from the response json and show the image in my tableview. But the problem is tableview is showing same image for all the tableview cell. Here is my tableview code:

enterfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array_product_name.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CellTableViewCell
    
    cell?.productName.text = array_product_name[indexPath.row]
    cell?.productPrice.text = array_product_price[indexPath.row]
    let imageU = UserDefaults.standard.string(forKey: "url")
    cell?.productImageLbl.sd_setImage(with: URL(string: imageU!))
    return cell!
}

The problem i am facing is, it's printing same image for all the table view cell.

Here is my Alamofire code:

Alamofire.request("http://192.168.80.21:3204/api/product/get_all_products", headers: headers).responseJSON { [self]
        response in
   
        switch response.result {
        case .success:
            
            let myresponse = try? JSON(data: response.data!)
            print("hello\(myresponse as Any)")
            
            let resultArray = myresponse
            self.array_product_name.removeAll()
            
            for i in resultArray!.arrayValue{
                let product_name = i["product_name"].stringValue
                self.array_product_name.append(product_name)
                
                let product_price = i["price"].stringValue
                self.array_product_price.append(product_price)
                
                let product_des = i["description"].stringValue
                self.array_product_description.append(product_des)
                
                let product_comName = i["company_name"].stringValue
                self.array_product_compamnyName.append(product_comName)
                
                let product_image  = i["image_url"].stringValue
                self.array_product_image.append(product_image)
                
                self.image_array = product_image
                print("Test1:- \(self.image_array)")
                UserDefaults.standard.setValue(image_array, forKey: "url")
            }
            
        case .failure(_):
            print(Error.self)
        }
        self.tableView.reloadData()
    }

You only save last value array url image form array value resultArray. If you want to save array url image, you can use:

  let defaults = UserDefaults.standard
  defaults.set(array, forKey: "SavedUrlStringArray")

In tableview:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CellTableViewCell
       cell?.productName.text = array_product_name[indexPath.row]
       cell?.productPrice.text = array_product_price[indexPath.row]

       let defaults = UserDefaults.standard
       let arrayImage = defaults.stringArray(forKey: "SavedUrlStringArray") ?? [String]()
     
       cell?.productImageLbl.sd_setImage(with: URL(string: arrayImage[indexPath.row]))
       return cell!
 }

However you can also use "array_product_image",there is no need to save the array value with userdefault.

Alamofire.request("http://192.168.80.21:3204/api/product/get_all_products", headers: headers).responseJSON { [self]
    response in

    switch response.result {
    case .success:
        
        let myresponse = try? JSON(data: response.data!)
        print("hello\(myresponse as Any)")
        
        let resultArray = myresponse
        self.array_product_name.removeAll()
        
        for i in resultArray!.arrayValue{
            let product_name = i["product_name"].stringValue
            self.array_product_name.append(product_name)
            
            let product_price = i["price"].stringValue
            self.array_product_price.append(product_price)
            
            let product_des = i["description"].stringValue
            self.array_product_description.append(product_des)
            
            let product_comName = i["company_name"].stringValue
            self.array_product_compamnyName.append(product_comName)
            
            let product_image  = i["image_url"].stringValue
            self.array_product_image.append(product_image)
        }
         self.tableView.reloadData()
    case .failure(_):
        print(Error.self)
    }
}


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

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CellTableViewCell

    cell?.productName.text = array_product_name[indexPath.row]
    cell?.productPrice.text = array_product_price[indexPath.row]
    cell?.productImageLbl.sd_setImage(with: URL(string: array_product_image[indexPath.row]))
    return cell!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM