当我插入/删除行时,为什么UITableView内容会抽动?

时间:2019-02-06 12:39:11

标签: swift uitableview uitableviewrowaction uitableviewsectionheader uitableviewautomaticdimension

我正在使用带有2种类型的单元格的TableView开发屏幕:“单项”单元格和“组项”单元格。 “单一”一个很简单-其视图为入口视图模型。 “组条目”单元必须显示有关多个条目的信息,并且必须能够显示/隐藏此详细信息。 我已通过以下方式解决了该问题:当用户单击“组”单元格时-我在调用方法之一:openGroup或closeGroup取决于isOpen参数,在这些函数中,我在TableView中的“组”单元格:

public func addRows(_ entries: [HistoryViewModelProtocol], at indexPaths: [IndexPath]) {
    for i in 0..<entries.count {
        let entry = entries[i]
        let indexPath = indexPaths[i]
        self.sections[indexPath.section].items.insert(entry, at: indexPath.row)
    }
    self.tableView.beginUpdates()
    self.tableView.insertRows(at: indexPaths, with: self.animation)
    self.tableView.endUpdates()
}

public func removeRows(_ rows: Range<Int>, in section: Int) {
    let indexPaths = rows.compactMap({ IndexPath(row: $0, section: section) })
    self.sections[section].items.removeSubrange(rows)
    self.tableView.beginUpdates()
    self.tableView.deleteRows(at: indexPaths, with: self.animation)
    self.tableView.endUpdates()
}

func openGroup(at indexPath: IndexPath) {
    guard let group = self.sections[indexPath.section].items[indexPath.row] as? HistoryGroupViewModel else {
        return
    }
    for i in 0..<group.items.count {
        let entry = group.items[i]
        let newIndexPath = IndexPath(row: indexPath.row + i + 1, section: indexPath.section)
        self.addRows([entry], at: [newIndexPath])
    }
}

func closeGroup(at indexPath: IndexPath) {
    guard let group = self.sections[indexPath.section].items[indexPath.row] as? HistoryGroupViewModel else {
        return
    }
    let rows = CountableRange(uncheckedBounds: (lower: indexPath.row + 1, upper: indexPath.row + 1 + group.items.count))
    removeRows(rows, in: indexPath.section)
}

但是有一个问题-当我插入或删除行时,TableView抽搐的所有内容:example movie

为什么?如何解决?

0 个答案:

没有答案