ios - 添加超过 2 个相同的单元格(动态 tableView)时图像显示不佳

标签 ios swift uitableview

在我的系统中,我以编程方式构建我的单元格,并在单元格中设置我需要的内容与我的数组(我的 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) 一个包含用户调查类的数组。我从这个细胞开始 build 我的细胞。

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 这样的框架带有图片的 url,但什么也没有。

为什么我的代码没有对正确单元格中的图像进行排序?

最佳答案

好吧,我错了……我用这个framework为我的 imageView 绘制异步模糊,但是这个插件保留了模糊图像的缓存。所以我只是删除了模糊的缓存,这解决了问题。

关于ios - 添加超过 2 个相同的单元格(动态 tableView)时图像显示不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094911/

相关文章:

iOS/Xcode : Name button created in storyboard name so it can be disabled

operators - 无法为元组定义一元运算符

ios - 如何在 Swift 中根据上一个 Controller 中选定的单元格值加载数组

swift - 您能否验证用户最近是否已在 Firebase (Swift) 上进行了身份验证?

ios - 如何在UITableView中实现搜索功能?

ios - 在 Spritekit 中使用 UITableView

ios - UItabar不能变色或透明

ios - Multipeer Connectivity Framework - Lost Peer 留在 session 中

ios - 如何在 prepareForSegue 中的 UITapGestureRecognizer 中点击并捕获其中的 UIImage 时获取自定义表格 View 单元格的索引路径

ios - 我需要将哪些 serviceUUID 传递给 retrieveConnectedPeripheralsWithServices 以快速获取任何连接的设备?