My attempts to insert cell into the end of the section in static tableView were successful. But when I was trying to insert cell into the middle of section I failed.
Here an exception that I’ve got: ‘NSRangeException’, reason: '* -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 … 1]'**
How can I resolve it?
Here that section:
Here is the code of relevant view controller:
// MARK: - Methods
func toggleStartDatePickerVisibility() {
let indexPathDatePicker = IndexPath(row: 1, section: 3)
if startDatePickerVissible {
startDatePickerVissible = false
tableView.deleteRows(at: [indexPathDatePicker], with: .top)
} else {
startDatePickerVissible = true
tableView.insertRows(at: [indexPathDatePicker], with: .top)
tableView.scrollToRow(at: indexPathDatePicker, at: .bottom, animated: true)
}
}
func toggleExpireDatePickerVisibility() {
let row = startDatePickerVissible ? 3 : 2
let indexPathDatePicker = IndexPath(row: row, section: 3)
if expireDatePickerVissible {
expireDatePickerVissible = false
tableView.deleteRows(at: [indexPathDatePicker], with: UITableView.RowAnimation.top)
} else {
expireDatePickerVissible = true
tableView.insertRows(at: [indexPathDatePicker], with: UITableView.RowAnimation.top)
tableView.scrollToRow(at: indexPathDatePicker, at: .bottom, animated: true)
}
}
// MARK: - Table view source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 3 {
switch (startDatePickerVissible, expireDatePickerVissible) {
case (true, true):
return 4
case (false, true), (true, false):
return 3
case (false, false):
return 2
}
} else {
return super.tableView(tableView, numberOfRowsInSection: section)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard indexPath.section == 3 else {
return super.tableView(tableView, cellForRowAt: indexPath)
}
switch (startDatePickerVissible, expireDatePickerVissible, indexPath.row) {
case (true, _, 1), (false, true, 2), (true, true, 3):
return datePickerCell
default:
return super.tableView(tableView, cellForRowAt: indexPath)
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard indexPath.section == 3 else {
return super.tableView(tableView, heightForRowAt: indexPath)
}
switch (startDatePickerVissible, expireDatePickerVissible, indexPath.row) {
case (true, _, 1), (false, true, 2), (true, true, 3):
return 217
default:
return super.tableView(tableView, heightForRowAt: indexPath)
}
}
// MARK: - TableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
datePicker.becomeFirstResponder()
guard indexPath.section == 3 else { return }
switch (startDatePickerVissible, expireDatePickerVissible, indexPath.row) {
case (_, _, 0):
toggleStartDatePickerVisibility()
case (false, _, 1), (true, _, 2):
toggleExpireDatePickerVisibility()
default:
break
}
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
guard indexPath.section == 3 else { return nil }
switch (startDatePickerVissible, expireDatePickerVissible, indexPath.row) {
case (_, _, 0), (false, _, 1), (true, _, 2):
return indexPath
default:
return nil
}
}
override func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath) -> Int {
var newIndexPath = indexPath
if indexPath.section == 3 && indexPath.row == 2 {
newIndexPath = IndexPath(row: 0, section: 3)
} else if startDatePickerVissible, indexPath.section == 3 && indexPath.row == 1 {
newIndexPath = IndexPath(row: 0, section: 3)
}
return super.tableView(tableView,
indentationLevelForRowAt: newIndexPath)
}
}