问题描述
我正在尝试从响应json中获取图片网址,并在我的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。 如果要保存数组网址图像,可以使用:
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!
}