ios - 单元格背景颜色清除插入行时的奇怪行为

标签 ios swift uitableview

不是重复的:
问题/答案是关于改变颜色的。我可以更改单元格的颜色,但在使用“颜色”.clear 并插入单元格时遇到问题,单元格正在“重置”。

每当我将单元格的背景颜色设置为 .clear 并在表格中插入一行时,单元格将自动“重置”到其原始状态。当背景颜色具有值时,不会发生这种情况。

例如:

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)
    }
}

我的 View Controller :

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)
}

问题:
如何将单元格的背景颜色设置为 .clear 并在不重置单元格的情况下插入一行?我想要这个是因为在我插入单元格后,我想将原始单元格动画化回其原始位置。

编辑:
设置 backgroundColor 的颜色仅在 iOS 9 中有效。在 iOS 10 中它仍然以某种方式重置单元格..

最佳答案

将您的插入内容更改为:

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

关于ios - 单元格背景颜色清除插入行时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42940223/

相关文章:

iphone - 创建自定义日历表 iOS 的一些问题

xcode - 如何使环绕文本字段填满整个窗口?

ios - 快速获取点击的 ImageView

ios - 更新父类(super class)的 TableView

iphone - 如何判断 UITableView 动画何时完成?

ios - 使自定义单元格 100% 透明

ios - 如何在 objective-c 的 uitableview 中显示分组数据

ios - 按函数过滤带有谓词的对象数组

ios - 减小 UITableViewRowAction 的字体大小

swift - 如何将 `substring` 更改为 swift 4 中的等效新版本?