![](/img/trans.png)
[英]SDWebImage not loading images Until I scroll the tableView in Swift
[英]How to use SDWebImage to show images in tableview Swift?
我試圖從響應 json 中獲取圖像 url 並在我的 tableview 中顯示圖像。 但問題是 tableview 為所有 tableview 單元格顯示相同的圖像。 這是我的tableview代碼:
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!
}
我面臨的問題是,它為所有表格視圖單元格打印相同的圖像。
這是我的 Alamofire 代碼:
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()
}
您只保存最后一個值數組 url 圖像形式的數組值 resultArray。 如果要保存數組 url 圖像,可以使用:
let defaults = UserDefaults.standard
defaults.set(array, forKey: "SavedUrlStringArray")
在表視圖中:
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!
}
但是您也可以使用“array_product_image”,無需使用用戶默認值保存數組值。
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!
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.