【问题标题】:reload tableview without image in cell reloads重新加载单元格中没有图像的表格视图重新加载
【发布时间】: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 reloadData from在cellForRowAt 内是非常非常糟糕
  • 是的......这是各种令人讨厌的事情。看起来您将无限地进行 API 调用并重新加载表。您可能应该重新考虑一下:-\。
  • @vadian 因为每当用户到达 tableview 的最后一个时,我都会更新 tableview ,新数据会添加到 tableview ...还有其他方法可以做同样的事情吗?

标签: swift tableview tableviewcell reloaddata kingfisher


【解决方案1】:

您正在使用已经处理图像缓存的Kingfisher。重新加载 tableView 时加载新图像应该只是读取缓存。

您需要删除此行中的.forceRefresh

cell.pizzaimg.kf.setImage(with: imgurl, options: [.downloadPriority(Float(indexPath.row)),.transition(.flipFromTop(1.0)),.forceRefresh,.processor(processor)])

根据文档,forceRefreshignore 缓存并再次从 URL 下载。你可以看到这个here

强制刷新

如果设置,Kingfisher 将忽略缓存并尝试为资源触发下载任务。

【讨论】:

  • 我需要 forcerefresh 因为服务器上的图像已更新,除此之外,我想以某种方式为我不使用 kingfisher 的其他项目处理这个问题
  • 您现在的问题是如何防止在重新加载 tableView 时加载 UIImage。方法是通过cache。如果没有Kingfisher,您仍然需要设置一些cache 来执行此操作。其次,您说您需要 forceRefresh 但您不想在重新加载 tableView 时重新加载图像,这对您提出的问题没有意义。如果您需要在一定时间后刷新它,那么您可以在图像上设置一些缓存过期时间。
  • 我不能只添加新数据而不重新加载整个 tableview 吗?所以我以前的单元格将保留并且不会加载
  • 你不能这样做。 UITableViewCell 正在被重用。因此,现在不可见的是被重用为正在出现的新cell。这是苹果设计的。 UITableView 无法做到这一点。
猜你喜欢
  • 2019-03-11
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
相关资源
最近更新 更多