【发布时间】:2017-03-21 14:36:52
【问题描述】:
我有一个 tableview 和其中的单元格,它有 imageview,它从服务器获取图像,每当用户向下滚动到 tableview 的末尾时,tableview 的数据就会重新加载,但我的问题是单元格图像将在 tableview 时再次从服务器下载图像重新加载,这是我的代码:
let cell = tableView.dequeueReusableCell(withIdentifier: "pizzacell", for: indexPath) as! CustomPizzaCell
let imgurl = URL(string: "\(BaseUrl)//assests/products/imgs/\(MainMenu.cellarray[indexPath.row].img)")
cell.pizzaimg.kf.setImage(with: imgurl, options: [.downloadPriority(Float(indexPath.row)),.transition(.flipFromTop(1.0)),.forceRefresh,.processor(processor)])
if indexPath.row == MainMenu.cellarray.count-1 {
if !isendoftable {
MainMenu.start += 5
MainMenu.end += 5
var parameters: Parameters = ["START": "\(MainMenu.start)"]
parameters["END"] = "\(MainMenu.end)"
parameters["TYPE"] = "\(MainMenu.type)"
print(parameters)
ApiManager.request(Address: "\(BaseUrl)/admin/Android/getLastProducts", method: .post, parameters: parameters) { json in
self.stopAnimating()
if json == nil {
let popupDialog = PopupDialog(title: "خطای دسترسی به سرور", message: "")
let btn = DefaultButton(title: "تلاش مجدد"){}
btn.titleFont = UIFont(name: "IRANSans(FaNum)", size: 15)
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
btn.titleFont = UIFont(name: "IRANSans(FaNum)", size: 17)
}
popupDialog.addButton(btn)
self.present(popupDialog, animated: true, completion: nil)
}
else {
if (json?.array?.isEmpty)! {
MainMenu.start = 0
MainMenu.end = 5
self.isendoftable = true
}
for item in (json?.array)! {
let piiiza = MainMenu.cell(id: item["id"].description, title: item["title"].description, img: item["img"].description)
MainMenu.cellarray.append(piiiza)
}
tableView.reloadData()
}
}
}
}
return cell
【问题讨论】:
-
猜测:
.forceRefresh不使用缓存,而是重新加载? -
你为什么要从
cellForRowAt中调用reloadData?非常非常糟糕。 -
基本上您需要将图像添加到数据源并检查 does-the-image-exist 我完全同意 rmaddy:Calling
reloadDatafrom在cellForRowAt内是非常非常糟糕。 -
是的......这是各种令人讨厌的事情。看起来您将无限地进行 API 调用并重新加载表。您可能应该重新考虑一下:-\。
-
@vadian 因为每当用户到达 tableview 的最后一个时,我都会更新 tableview ,新数据会添加到 tableview ...还有其他方法可以做同样的事情吗?
标签: swift tableview tableviewcell reloaddata kingfisher