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..