2

I have a UITableViewController, with a static 5 sections and 2 rows per sections, but the last section with 1 row.

I've created the table in storyboards and set the number of sections and rows via storyboards. In the protocol functions I've also set the correct number of rows and sections:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 5
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (section == 4) {
        return 1
    } else {
        return 2
    }
}

However, I get this error:

'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

I believe this error is related to the numberOfSections function because if I change it from 5 to 0, it runs fine. I don't understand how the array of sections can be zero when I've defined it in storyboards to be 5. Is there a simple solution I'm not seeing?

I also have the delegate sources linked up, and my data source is not any kind of array, as the entire table was created in storyboards, and contains labels, etc.

EDIT

Code for entire view controller:

import UIKit

class RideSummaryTableViewController: UITableViewController {

@IBOutlet var nameLabel: UILabel!
@IBOutlet var ratingLabel: UILabel!
@IBOutlet var originStreetLabel: UILabel!
@IBOutlet var originCityLabel: UILabel!
@IBOutlet var destStreetLabel: UILabel!
@IBOutlet var destCityLabel: UILabel!
@IBOutlet var rateLabel: UILabel!
@IBOutlet var paymentButton: UIButton!

var paymentText = "Request Payment"

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true

    self.paymentButton.setTitle(paymentText, for: .normal)

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
    return 5
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (section == 4) {
        return 1
    } else {
        return 2
    }
}

/*
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

    // Configure the cell...

    return cell
}
*/

/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete the row from the data source
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */

}
Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30
Nick H
  • 217
  • 4
  • 19
  • Just to be sure, have you selected Static cells in the storyboard? You haven't populated dynamic cells by any chance. Although I don't think its possible to create sections with dynamic cells. Could you post the whole controller? – Devster101 Mar 18 '17 at 14:44
  • Yes I've selected Static cells in the storyboard. Posting the entire view controller class now. – Nick H Mar 18 '17 at 15:07

1 Answers1

0

I just had the same problem and it took me some time to find my error.

My mistake was that there was still a cell left on the storyboard. After I have deleted the cell and the corresponding section/group it worked how it should.

André Kuhlmann
  • 4,378
  • 3
  • 23
  • 42