簡體   English   中英

帶有透明單元格的UITableView在重載數據時與舊單元格重疊

[英]UITableView with transparent cells overlap old cells when reloading data

我有一個具有透明背景色的UITableView和也具有透明背景色的單元格。 當我用以下命令重新加載tableView時:

dataSource = some new data
tableView.reloadData()

我可以看到新單元格與舊單元格重疊。 透明單元互相重疊

我確實嘗試過使用

tableView.beginUpdates()
// remove all rows here
change data source
// insert new rows here
tableView.endUpdates()

但它沒有用。 我也嘗試了tableView.reloadRowsAtIndexPath(...)但還是沒有運氣。

最后,我將所有單元格和表格視圖設置為在重繪時清除圖形上下文,但無法解決此問題。

任何幫助將不勝感激。


我的單元格創建功能:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("suggestioncell")
    cell.backgroundColor = UIColor.whiteColor().alpha(0.1)
    cell.textLabel?.text = (suggestions![indexPath.row] as! SVPlacemark).formattedAddress
    cell.clearsContextBeforeDrawing = true
    cell.contentView.clearsContextBeforeDrawing = true
    return cell
}

嘗試覆蓋UITableViewCell子類中的prepareForReuse ,然后在那里重新設置內容。

這是有關文檔的內容:

准備一個可重用的單元格,以供表視圖的委托重用。

如果UITableViewCell對象是可重用的(即,它具有重用標識符),則在從UITableView方法dequeueReusableCellWithIdentifier:返回對象之前,將調用此方法。 出於性能原因,您僅應重置與內容無關的單元格屬性,例如Alpha,編輯和選擇狀態。 重新使用單元格時,tableView:cellForRowAtIndexPath:中的表視圖委托應始終重置所有內容。 如果單元對象沒有關聯的重用標識符,則不會調用此方法。 如果重寫此方法,則必須確保調用超類實現。

自定義UITableViewCell類:

class customCell: UITableViewCell {
    override func prepareForReuse() {
        self.textLabel?.text = nil
    }
}

在您的cellForRowAtIndexPath方法中:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("suggestionCell", forIndexPath: indexPath) as! customCell
    cell.backgroundColor = UIColor.whiteColor().alpha(0.1)
    cell.textLabel?.text = (suggestions![indexPath.row] as! SVPlacemark).formattedAddress
    return cell
}

並且,當然,在您的XIB / Storyboard中,將單元格類設置為CustomCell,並設置其重用標識符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM