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

时间:2018-11-01 03:37:17

标签: 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)。从这个开始,我就开始建立自己的细胞。

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之类的框架来处理图像的网址,但一无所获。

任何人都可以知道为什么我的代码无法在优质单元格中对图像进行排序吗? 感谢您的答复。

1 个答案:

答案 0 :(得分:0)

好吧,我的错...我使用此framework向我的imageView绘制异步模糊,但是此插件保留了具有模糊的图像的缓存。因此,我只删除了模糊和解决问题。