【问题标题】:the data mix in table view, how do I avoid it? swift表格视图中的数据混合,我该如何避免?迅速
【发布时间】:2019-07-08 03:32:28
【问题描述】:

我有一些麻烦,我有一个带有自定义单元格的表格视图,该单元格有一个图像视图,图像是从互联网下载的,当第一次加载数据时一切都很好,但是如果快速滚动屏幕,图像会混合。 这是我的代码

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell:FlinkerTableViewCell = tableView.dequeueReusableCell(withIdentifier: "flinkerCell") as! FlinkerTableViewCell
        cell.imgeCell.image = nil
        let row = indexPath.row
        let flinkerAux: Flinker = flinkers[row]
        if let stringURL = flinkerAux.image{
            if stringURL != "" {
                DispatchQueue.main.async {
                    cell.imgeCell.downloadedFrom(link: stringURL)
                    cell.imgeCell.contentMode = .scaleAspectFill
                }
                cell.imgeCell.setNeedsDisplay()
                cell.lblTitle.text = flinkerAux.fullName
                cell.lblSubtitle.text = flinkerAux.phone
            }else{
                cell.imgeCell.image = UIImage(named: "user_placeholder")
                cell.lblTitle.text = flinkerAux.fullName
                cell.lblSubtitle.text = flinkerAux.phone
            }
        }else{
            cell.imgeCell.image = UIImage(named: "user_placeholder")
            cell.lblTitle.text = flinkerAux.fullName
            cell.lblSubtitle.text = flinkerAux.phone
        }
        cell.imgeCell.layer.cornerRadius = 20
        cell.imgeCell?.clipsToBounds = true
        cell.selectionStyle = .none
        return cell
    }

我使用的是 swift 4.2,我尝试使用 DispathQueue 加载图像,但是当 table view 有这么多图像时,问题又回来了。 我该怎么办?

【问题讨论】:

    标签: swift image tableview cell


    【解决方案1】:

    这是一个众所周知的问题。我相信你可以通过 google 找到很多东西

    这里最简单的选择是使用任何库来下载图像,例如 AlamofireImage、Nuke、Kingfisher、SDWebImage

    UITableViewCell asynchronously loading images issue - Swift

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多