繁体   English   中英

当添加两个以上相同单元格时,图像无法很好地显示(动态tableView)

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

使用我的系统,我可以通过编程方式构建单元格,并与数组(tableView的数据源)相比,设置单元格中所需的内容。

当我在tableView上添加3个或更多单元格时出现问题。 文本很好,但是图像没有出现在正确的单元格中。 我认为这是TableView系统缓存(重用)的问题。 我在该论坛上关注了几处帖子,以解决此问题,但没有任何效果。

这是我在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

这就是我如何更新包含用户调查类的数组的源数据(lobbySurvey)。 从这开始,我开始建立自己的细胞。

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)

这是clearImage函数:

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

和我的setImage函数:

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

我试图像这样清空我的imageView:

surveyWaiting.imageEmptyView.image = nil

但是不行。 我还尝试使用像Nuke这样的框架来处理图像的网址,但一无所获。

谁能想到为什么我的代码无法在优质单元格中对图像进行排序? 多谢您的回覆。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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