简体   繁体   中英

Image not display well when added more than 2 same cells (dynamic tableView)

With my system I build my cell programmatically and set what I need in the cell compared to my array (data source of my tableView).

The problem appear when i add 3 cells or more on my tableView. The text is good but the image does not appear in the correct cell. I think this is a problem of the cache of the TableView system (reuse). I follow several post in this forum to fix that but nothing work.

This is my code in cellForRowAt :

let waitingCell = tableView.dequeueReusableCell(withIdentifier:"waiting", for: indexPath) as! CardCellWaiting
var cellToReturn : UITableViewCell!
let currentSurvey = user.lobbySurvey[indexPath.row]
let state : UserSurvey.state = currentSurvey.stateSurvey
switch state{
case .surveyWaiting:
    cellToReturn = waitingCell
    waitingCell.drawCard() // draw the card programmatically if needed
    waitingCell.clearImage() // look below to see the function
    waitingCell.setId(id: currentSurvey.id)
    waitingCell.setImage(image : currentSurvey.picture)
    waitingCell.setTimeLeft(timeLeft: currentSurvey.timeLeft)
    waitingCell.delegate = self
    waitingCell.delegateCard = self
}
return cellToReturn

this is how I update my source data (lobbySurvey) an array who contain User Survey classes. I build my cells since this one.

user.lobbySurvey.remove(at: 0)
self.tableView.deleteRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
let surveyWaiting = UserSurvey(stateOfSurvey: .surveyWaiting)
surveyWaiting.picture = image
if let url = json["imageUrl"].string {
    surveyWaiting.pictureUrl = url
}
if let timeLeft = json["timeLeft"].string {
    surveyWaiting.timeLeft = timeLeft
}
if let surveySelfId = json["surveySelfId"].string {
    surveyWaiting.id = Int(surveySelfId)
}
let rowToInsert = self.getRowToInsert(typeOfState: .surveyWaiting)
user.counterSurvey += 1
user.lobbySurvey.insert(surveyWaiting, at: rowToInsert)
self.tableView.insertRows(at: [IndexPath(row: rowToInsert, section: 0)], with: .fade)

this is the clearImage function :

func clearImage(){
    surveyWaiting.imageEmptyView.image = #imageLiteral(resourceName: "backgroundEmptyImage")
}

and my setImage function :

func setImage(image : UIImage){
    surveyWaiting.imageEmptyView.image = image.resized(targetSize: CGSize(width:100,height:125))
}

I've try to empty my imageView like :

surveyWaiting.imageEmptyView.image = nil

But don't work. I've also try to use a framework like Nuke with the url of the image, but nothing.

Can anyone have an idea of why my code not order the image in the good cell ? Thanks to your reply.

好的,我的错误...我使用此框架在我的imageView上绘制异步模糊,但是此插件保留了具有模糊的图像的缓存。因此,我只删除了模糊的缓存即可解决问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM