1

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.

trever
  • 961
  • 2
  • 9
  • 28
  • Looks to me like you're trying to reload _section_ 2 not row 2. Try `NSIndexSet(index: 0)` – brandonscript Apr 01 '16 at 02:46
  • Add a [exception breakpoint](http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode) so that we can see where is `NSRangeException`. – user3480295 Apr 01 '16 at 03:10

1 Answers1

0

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.

Lumialxk
  • 6,239
  • 6
  • 24
  • 47