簡體   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