2017-10-06 159 views
-1

我正在使用UITableView來顯示我的數據。圖像在滾動時消失UITableView

我的數據由圖像和文字組成。問題是當我滾動UITableView圖像消失。

這是我的代碼:

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return msgs.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! collCell 
    let msg = msgs[indexPath.row] 
    var decodeImgURL = "" 
    cell.lbl1.text = msg.content 

    decodeImgURL = msg.imgurl 

    if decodeImgURL == "none" { 
     cell.img.isHidden = true 
    } else { 

     let dataDecoded : Data = Data(base64Encoded:decodeImgURL,options: .ignoreUnknownCharacters)! 
     let decodedImage = UIImage(data: dataDecoded) 
     cell.img.image = decodedImage 

    } 

    return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

    let msg = msgs[indexPath.row] 

    if msg.imgurl == "none" { 
     return 64 
    } else { 
     return 207 
    } 

} 
+0

'lbl1'中的'msg.content'也消失了嗎? – thedjnivek

+0

你的形象甚至出現在第一位? –

+0

@thedjnivek否lbl1保持不變 –

回答

0

隨着UITableView,那是在內存中的唯一細胞是可見的單元格。當您將表格單元格從屏幕上滾動時,它會從內存中移除。這有助於更快地滾動表格視圖。您面臨的問題是,當您將表格單元格從屏幕上滾動時,單元格(以及圖像)將從內存中解除分配。當您將單元格滾動回屏幕時,它會再次分配到內存中。我認爲你的問題可能來自以下兩個問題之一:

1)你只需要等待幾秒鐘,在單元格中重新創建圖像。

2)也許在你的msgs數組中有個問題。如果幾秒鐘後圖像沒有出現,請嘗試在此行之後放置一個斷點:cell.img.image = decodedImage。如果在將屏幕移出屏幕後沒有達到斷點,然後返回屏幕,則問題是decodeImgURL(圖像的url)不正確。

+0

我已經把斷點放在'cell.img.image = decodeImage' yaa整個部分在向下滾動時調用,但它沒有設置我的形象 –

相關問題