I am trying to reload section and update the number of cells in a section, but I'm getting an error and crash whenever I try. Here is the code I am using:
import UIKit
import CalendarView
import SwiftMoment
class TimeAwayRequestTableViewController: UITableViewController {
@IBOutlet var calendarView: CalendarView!
var selectedDates : [Moment] = []
override func viewDidLoad() {
super.viewDidLoad()
calendarView.delegate = self
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnInt = 0
if section == 0 {
returnInt = 2
}
if section == 1 {
returnInt = 1
}
if section == 2 {
print(selectedDates.count)
returnInt = selectedDates.count
}
return returnInt
}
}
extension TimeAwayRequestTableViewController : CalendarViewDelegate {
func calendarDidSelectDate(date: Moment) {
selectedDates.append(date)
print(selectedDates)
let section = NSIndexSet(index: 2)
self.tableView.beginUpdates()
self.tableView.reloadSections(section, withRowAnimation: .Automatic)
self.tableView.endUpdates()
}
func calendarDidPageToDate(date: Moment) {
print(date)
}
}
So basically when the date is tapped in the calendar, I add it to the Array and then update the number of cells. I haven't gotten to the point where I configure the cell content, right now I'm just trying to make sure this is working like i want. The error I get is:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
But i don't understand why because it counts and shows 2. So I'm confused.
I bet you used some arrays(including selectedDates
) in func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
and it was beyond of bounds.Please check your data.