ui table 查看数据源

ui table view data source

我该怎么办?

这是我的代码 ui table 查看行路径的单元格

请注意,这些图像来自 api 调用

when I keep scroll in the table view till the cell disappear the image disappear too its like I lose the image source just its background still appear

我有一个 uiTableView 有很多单元格,每个单元格都有一个图像,当我向下滚动直到单元格消失时,那些单元格中的图像消失了!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    cell = blockListTable.dequeueReusableCell(withIdentifier: "BlockCell", for: indexPath) as! BlockTableViewCell
    cell.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? UIColor(hex: 0xEEEEEE) : UIColor(hex:0x001638 )
    cell.index = indexPath
    imageUrl = imageUrl + blockUsers[indexPath.row].profilePicture
    if blockUsers[indexPath.row].profilePicture == "" {
        cell.profilePicture.image = UIImage(named: "blockedUser")
    }
    else {
        
        cell.profilePicture.kf.setImage(with: URL(string: imageUrl))
    }
    
   
    return cell
}

我在这段代码中发现了两个紧迫的问题。

  1. 当您将“blockedUser”图像设置为图像视图时,如果单元格被重复使用,Kingfisher 可能仍在后台下载图像。这会覆盖您的静态图像。

    设置静态图片前一定要调用cancelDownloadTask

    if blockUsers[indexPath.row].profilePicture == "" {
        cell.profilePicture.kf.cancelDownloadTask() // (!)
        cell.profilePicture.image = UIImage(named: "blockedUser")
    } else {
        cell.profilePicture.kf.setImage(with: URL(string: imageUrl))
    }
    
  2. 每次调用 cellForRowAt 时,以下行总是将内容附加到 字段 ,这最终肯定会构建一个无效的 URL永远不会固定。

    imageUrl = imageUrl + blockUsers[indexPath.row].profilePicture
    

    因为没什么可继续的,我假设你有一个“base URL”存储在 字段,并且您想在给定用户的末尾附加一个文件名。

    如果是这种情况,您可能想改写这个,创建一个新的局部变量而不是覆盖该字段:

    let imageUrl = self.imageUrl + blockUsers[indexPath.row].profilePicture