I am having an issue with table view cell insertion and deletion. I basically have 1 tableView that can display 3 different arrays in it, based on a mode selected by the user.
When the user clicks on a different mode, I want to animate the initial cell removal and then animate the insertion of the other cells.
This is working when my arrays are really small (size of 5 or so) but when it gets larger than that I get crashes. Are we restricted from removing indexPaths that aren't on display in tableViews?
Anyway my code is as follows:
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)
As stated, when the arrays don't populate that many items, no issue is occurring. But i'm having an issue when the arrays are populated with what seems to be more than 5 items. Any help would be greatly appreciated.
Thank you for your time, Alan