有大量单元格时,“删除和插入行”崩溃

时间:2018-09-21 13:32:45

标签: ios swift uitableview tableview performbatchupdates

我在插入和删除表格视图单元格时遇到问题。我基本上有1个tableView,可以根据用户选择的模式在其中显示3个不同的数组。

当用户单击其他模式时,我要为初始单元格去除动画,然后为其他单元格的插入动画。

这在我的数组很小(大小为5左右)但大于它的大小时会崩溃。我们是否被禁止删除tableViews中未显示的indexPath?

无论如何,我的代码如下:

 func viewDidLoad() {
    let o1 = "1"
    let o2 = "2"
    let o3 = "3"
    array1 = [o1, o2, o2, o1, o3, o1, o2] 
    array2 = [o3, o2, o2, o1, o3, o1, o2, o2, o2, o1, o3, o1, o2]
    array3 = [o3, o1, o2, o2, o2, o1, o3, o1, o2]
}

func animateTableTransitions() {
    let array: [String]!
    let previousArray: [String]!
    let index = ModeEnum.index
    let previousIndex = ModeEnum.index //Custom enum that mains the index at which each mode is presented
    var indexPaths = [IndexPath]()
    var previousIndexPaths = [IndexPath]()
    switch (currentMode, previousMode) {
    case (.mode1, .mode2):
        array = array1
        previousArray = array2
    case (.mode1, .mode3):
        array = array1
        previousArray = array3
    case (.mode2, .mode1):
        array = array2
        previousArray = array1
    case (.mode2, .mode3):
        array = array2
        previousArray = array3
    case (.mode3, .mode1):
        array = array3
        previousArray = array1
    case (.mode3, .mode2):
        array = array3
        previousArray = array2
    default:
        return
    }

    for index in 0..<array.count {
        indexPaths.append(IndexPath(row: index, section: 0))
    }

    for index in 0..<previousArray.count {
        previousIndexPaths.append(IndexPath(row: index, section: 0))
    }

    if previousTranslationMode == .text && textModeOn {
        previousIndexPaths.append(IndexPath(item: textArray.count, section: 0))
    }

    let firstAnimationType: UITableViewRowAnimation = index < previousIndex ? .right : .left
    let secondAnimationType: UITableViewRowAnimation = index < previousIndex ? .left : .right

    tableView.performBatchUpdates({
        self.tableView.deleteRows(at: previousIndexPaths, with: firstAnimationType)
        self.tableView.insertRows(at: indexPaths, with: secondAnimationType)
    }, completion: nil)

如前所述,当数组没有填充那么多项目时,就不会发生任何问题。但是,当数组中似乎包含5个以上的项时,我遇到了一个问题。任何帮助将不胜感激。

感谢您的宝贵时间, 艾伦

0 个答案:

没有答案