You could solve this with a simple function
private func updateTable(count: Int, oldCount: Int, section: Int) {
if count > 0, count == (oldCount + 1) {
tableView.insertRows(at: [IndexPath.init(row: 0, section: section)], with: .right)
} else {
tableView.reloadData()
}
}
This would simplify the code to
private var notes: [String] = [] {
didSet {
updateTable(count: notes.count, oldCount: oldValue.count, section: 3)
}
}
Another option is to pass the arrays
private func updateTable(array: [Element], oldArray: [Element], section: Int) {
if array.count > 0, array.count == (oldArray.count + 1) {
tableView.insertRows(at: [IndexPath.init(row: 0, section: section)], with: .right)
} else {
tableView.reloadData()
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…