简体   繁体   中英

UITableView with transparent cells overlap old cells when reloading data

I have a UITableView with a transparent background color and cells which also have a transparent background color. When I reload my tableView with:

dataSource = some new data
tableView.reloadData()

I can see the new cells overlap the old ones. 透明单元互相重叠

I did try to use use

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

but it did not work. I tried as well tableView.reloadRowsAtIndexPath(...) but still no luck.

And finally I set all my cells and my table view to clear graphic context when redrawn but it did not manage to fix this issue.

Any help would be greatly appreciated.


My cell creation function:

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
}

Try overriding prepareForReuse in you UITableViewCell subclass, and reset content there.

Here's what the documentation says about that:

Prepares a reusable cell for reuse by the table view's delegate.

If a UITableViewCell object is reusable—that is, it has a reuse identifier—this method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:. For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. If the cell object does not have an associated reuse identifier, this method is not called. If you override this method, you must be sure to invoke the superclass implementation.

Custom UITableViewCell class:

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

In your cellForRowAtIndexPath method:

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
}

And, of course in your XIB/Storyboard, set the cell class to CustomCell, and set its reuse identifier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM