1

Not a duplicate:
The question / answer is about changing the color. I have no problem changing the color of the cell, rather I have a problem when having the "color" .clear and inserting a cell, the cell is being "reset".

Whenever I set the background color of my cell to .clear and I insert a row in the table, the cell will automatically "resets" to its original state. This doesn't happen when the background color has a value.

For example:

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    let gesture = UIPanGestureRecognizer()
    gesture.addTarget(self, action: #selector(handlePan))
    gesture.delegate = self

    addGestureRecognizer(gesture)

    backgroundColor = .clear
}

func handlePan(_ recognizer: UIPanGestureRecognizer) {
    if recognizer.state == .changed {
        let translation = recognizer.translation(in: self).x

        contentView.frame.origin.x += translation

        recognizer.setTranslation(.zero, in: self)
    }

    if recognizer.state == .ended {
        guard let index = delegate?.tableView.indexPath(for: self) else { return }

        delegate?.insert(index)
    }
}

My ViewController:

func insert(_ index: IndexPath) {
    let cell = tableView.cellForRow(at: index) as? PannableCell

    items.append("some")

    let path = IndexPath(row: index.row + 1, section: index.section)

    tableView.insertRows(at: [path], with: .fade)
}

Question:
How can I set my background color of my cell to .clear and insert a row without the cell being reset? I want this because after I've inserted the cell, I want to animate the original cell back to it's original position.

Edit:
setting the color of backgroundColor only works in iOS 9. In iOS 10 it still resets the cell somehow..

Henny Lee
  • 2,682
  • 3
  • 16
  • 31
  • Possible duplicate of [UITableViewCell show white background and cannot be modified on iOS7](http://stackoverflow.com/questions/18878258/uitableviewcell-show-white-background-and-cannot-be-modified-on-ios7) – kevin Mar 22 '17 at 01:38
  • Thanks for the great link. While it doesn't fix the problem when I want the color to be `.clear` it does so if I'm using another color (I used the background color of my table as a temp fix). – Henny Lee Mar 22 '17 at 01:44
  • Strangely if I change the `backgroundColor` to another color (for instance: `.gray`) the cell is still getting reset (while inserting a new cell) until the cell is getting reused. The answer in the link kkoltzau provided fixed this (when the color isn't `.clear`. – Henny Lee Mar 22 '17 at 01:46
  • @kkoltzau: it's not a duplicate. – Henny Lee Mar 22 '17 at 01:53

2 Answers2

1

change your insert to this:

tableView.beginUpdates()
tableView.insertRows(at: [path], with: .fade)
tableView.endUpdates()
Jay
  • 2,431
  • 1
  • 14
  • 26
1

Try to use beginUpdates() and endUpdates() on your tableView.

tableView.beginUpdates()
// insert, delete your rows here
tableView.endUpdates()

If you do not use these methods you can end up with all sorts of bugs including invalid row count, UI glitches etc.

If it doesn't help you can try to "dirty fix" it by setting the desired color in prepareForReuse method of your cell or in tableView:willDisplayCell:forRowAtIndexPath: delegate method (try to avoid dirty fixes like this).

Oleh Zayats
  • 2,233
  • 1
  • 14
  • 24
  • Thanks, unfortunately it doesn't work. Somehow when the color is `.clear` and inserting a cell it will always happen, even if the color is set in `willDisplayCell`. – Henny Lee Mar 22 '17 at 02:09
  • @HennyLee can you upload the project somewhere? hard to tell without seeing the actual code – Oleh Zayats Mar 22 '17 at 02:12
  • Unfortunately I can't, sorry. The project is not mine. The code provided should be able to reproduce this though. – Henny Lee Mar 22 '17 at 02:21