UITableview,SDWebImage,单元重用和UIImage问题

问题描述

在此处查看视频示例: https://imgur.com/a/vSBPjlP

具有从Internet异步下载的图像的表格视图。我能够使它们正确填充,而不会出现我自己的代码或SDWebImage问题。当我将更多数据分页到tableview的末尾时,我的问题开始了。分页确实有效。与所有文本数据一样,它们都可以正常工作并在整个tableview中正确保留。我遇到的问题是我的ImageView。正如您在视频中看到的那样,在分页之后,UIImageViews图像会将所有单元格从上一次调用更改为表视图末尾的最新单元格中的内容图像。是什么导致这种行为,为什么它仅发生在我的图像视图中,我该怎么做才能解决

我尝试将prepareForReuse中的imageview图像设置为nil(违反Apple的建议),并且产生相同的结果。

相关代码

API调用

 func downloadGamesByPlatformIDJSON(platformID: Int?,fields: String?,include: String?,pageURL: String?,completed: @escaping () -> () ) {
        var urlString : String?
        
        if pageURL == nil {
            urlString = "https://api.thegamesdb.net/v1/Games/ByPlatformID?apikey=\(apiKey)&id=\(platformID!)"
            
            if fields != nil {
                urlString = urlString! + "&fields=" + fields!
            }
            if include != nil {
                
                urlString = urlString! + "&include=" + include!
            }
            
        } else {
            urlString = pageURL!
        }
        let url = URL(string: "\(urlString!)")!
        var requestHeader = URLRequest.init(url: url)
        requestHeader.httpMethod = "GET"
        requestHeader.setValue("application/json",forHTTPHeaderField: "Accept")
        
        URLSession.shared.dataTask(with: requestHeader) { (data,response,error) in
            
            if error != nil {
                print("error = \(error)")
                completed()
            }
            
            if error == nil {
                do {
                    print("error = nil")
                    let json = String(data: data!,encoding: .utf8)
                    
                    print(json)
                    
                    if let jsonDecodedplatforms = try JSONDecoder().decode(ByPlatformIDData?.self,from: data!) {
                        let decodedJSON = jsonDecodedplatforms.data?.games
                        self.Boxart = jsonDecodedplatforms.include.Boxart
                        self.baseURL = jsonDecodedplatforms.include.Boxart.baseURL
                        self.page = jsonDecodedplatforms.pages
                        
     
                        self.games.append(contentsOf: decodedJSON!                
                            }
                    
                    dispatchQueue.main.async {
                        completed()
                    }
                } catch {
                    print(error)
                }
                
            }
            
        }.resume()
        
    }

分页

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        let contentHeight = scrollView.contentSize.height
        
        if offsetY > contentHeight - scrollView.frame.size.height {
            
            if !fetchingMore {
                beginBatchFetch() 
            }
        }
    }
    func beginBatchFetch() {
        fetchingMore = true
        print("fetching data")
        network.downloadGamesByPlatformIDJSON(platformID: nil,fields: nil,include: nil,pageURL: network.page?.next) {
            print("pagination successful")
            self.fetchingMore = false
            self.tableView.reloadData()
                      
                  }
        
    }

cellForRowAt

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! ViewControllerTableViewCell
        
        cell.configureCells()

        //creating game object
        let game1 : GDBGamesPlatform?
        game1 = network.games[indexPath.row]

 //retrieving the filename information based on the game id
        
        if network.Boxart?.data["\(game1!.id!)"]?[0].side == .front {
            print(network.Boxart?.data["\(game1!.id!)"]?[0].filename)
            frontimageName = network.Boxart?.data["\(game1!.id!)"]?[0].filename as! String
            
        } else if network.Boxart?.data["\(game1!.id!)"]?[0].side == .back {
            backImageName = network.Boxart?.data["\(game1!.id!)"]?[0].filename as! String
            
        }
        
        if network.Boxart?.data["\(game1!.id!)"]?.count == 2 {
        if network.Boxart?.data["\(game1!.id!)"]?[1].side == .front {
            frontimageName = network.Boxart?.data["\(game1!.id!)"]?[1].filename as! String
                       
        } else if network.Boxart?.data["\(game1!.id!)"]?[1].side == .back {
            backImageName = network.Boxart?.data["\(game1!.id!)"]?[1].filename as! String
                       
        }
        }
   
        
        
        //creating image url string
        var imageUrlString = network.baseURL!.small + frontimageName
        print(imageUrlString)
        let imageURL = URL(string: imageUrlString)
        
        
        
        
        //if data exists for the front cover image download it,otherwise show default image
        if frontimageName != nil {
            
            cell.loadCoverImageWith(urlString: imageUrlString)
            
        } else {
            cell.tableViewCoverImage.image = UIImage(named: "noArtnes")

        }
        
        return cell
        
    }

import UIKit
import SDWebImage

class ViewControllerTableViewCell: UITableViewCell {
    @IBOutlet weak var tableViewCoverImage: UIImageView!
    @IBOutlet weak var tableViewGameName: UILabel!
    @IBOutlet weak var tableViewGenreLabel: UILabel!
    @IBOutlet weak var tableViewAgeratingLabel: UILabel!
    @IBOutlet weak var tableViewCompanyLabel: UILabel!
    @IBOutlet weak var tableViewReleaseDateLabel: UILabel!
    @IBOutlet weak var backgroundCell: UIView!
    @IBOutlet weak var tableViewCoverRearImage: UIImageView!
    @IBOutlet weak var gameCartimage: UIImageView!
    
    
    override func prepareForReuse() {
           super.prepareForReuse()
        
        tableViewCoverImage.sd_cancelCurrentimageLoad()
        tableViewCoverImage.image = nil
             }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
                                                  
    }

    override func setSelected(_ selected: Bool,animated: Bool) {
        super.setSelected(selected,animated: animated)

        // Configure the view for the selected state
    }
    
    func loadCoverImageWith(urlString: String) {
        let imageURL = URL(string: urlString)
       
            
            self.tableViewCoverImage.sd_setimage(with: imageURL,placeholderImage: UIImage(named: "noArtnes"),options: SDWebImageOptions.highPriority) { (image,error,cacheType,url) in
                
                if let error = error {
                    print("Error downloading the image.  Error Description: \(error.localizedDescription)")
                } else {
                    let edgeColor = self.tableViewCoverImage.image?.edgeColor()
                    self.tableViewCoverImage.layer.shadowColor = edgeColor!.cgColor
                }
            }

        
        
        
    }
    

    func configureCells() {
        tableViewCoverImage.layer.shadowOffset = CGSize(width: -5,height: 8)
        tableViewCoverImage.layer.shadowRadius = 8
        tableViewCoverImage.layer.shadowOpacity = 0.8
        tableViewCoverImage.layer.cornerRadius = 10
        tableViewCoverImage.clipsToBounds = false
        tableViewCoverImage.layer.masksToBounds = false
        backgroundCell.layer.shadowOffset = CGSize(width: 0,height: 5)
        backgroundCell.layer.shadowRadius = 5
        backgroundCell.layer.shadowOpacity = 0.1
        backgroundCell.layer.cornerRadius = 10
        if self.traitCollection.userInterfaceStyle == .light {
            backgroundCell.layer.shadowColor = UIColor.black.cgColor
            backgroundCell.layer.backgroundColor = UIColor.white.cgColor

        } else {
            backgroundCell.layer.shadowColor = UIColor.white.cgColor
            backgroundCell.layer.backgroundColor = UIColor.black.cgColor

    }
    
   

}
}

解决方法

对API的第二次及以后的调用会将游戏的结果附加到数组中,但会覆盖boxart的结果。后面几页的结果将不包括前面几页的boxart。

因此,您的Boxart数据不再包含早期页面的插图。您从最后一项获取图像的原因是,变量frontImageName和backImageName具有错误的作用域,它们应该位于tableView(:cellForRowAt:)

您需要通过调用Dictionary.merge(_:uniquingKeysWith :)与现有的Boxart词典合并 API调用的结果。有关详情,请参见https://developer.apple.com/documentation/swift/dictionary/3127171-merge

,

这是可重用性问题。尝试在cellForRowAtIndex中更改此行。

tableview.register(UINib(nibName: "TableViewCell",bundle: nil),forCellReuseIdentifier: "cell\(indexPath.row)")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell\(indexPath.row)",for: indexPath) as! ViewControllerTableViewCell