The Cubear Guy
The Cubear Guy

Reputation: 381

Static TableView gets NSRangeException

I have a UITableViewController with static cells, and when I run the app and click on the button that leads to it, I get a SIGABRT signal in AppDelegate.

I tried to find unused outlets, but it didn't work.

Here is the Console Log:

Console Log

The UITableViewController Code:

import UIKit
import os.log

class SettingsTableViewController: UITableViewController {

    // MARK: Properties
    @IBOutlet weak var noteDisplayKindSwitch: UISwitch!

    override func viewDidLoad() {
        super.viewDidLoad()

        notedisplayKindSwitch.setOn(Settings._displaynotesAsNames, animated: false)
    }

    @IBAction func ChangeNoteDisplayKind(_ sender: UISwitch) {
        Settings._displayNotesAsNames = sender.isOn

    // MARK: - Table view data source

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
}

The UITableViewController in the Storyboard:

The UITableViewController in the Storyboard

The Connections:

The Connections

What am I doing wrong? Thanks.

Upvotes: 0

Views: 78

Answers (2)

Gereon
Gereon

Reputation: 17882

Somewhere in your tableView:cellforRowAtIndexPath method, there's an empty array but you're trying to access the element at index 1.

Upvotes: -1

vacawama
vacawama

Reputation: 154711

You are using static cells. There is no reason to implement numberOfSections and numberOfRowsInSection because those are specified by the static layout in the Storyboard.

Because you have implemented:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3
}

you are telling iOS that every section has 3 rows, which is a lie. So iOS tries to access the second row of your first section and crashes with array index out of range because that section has just 1 row.

So, delete the implementations of numberOfSections and numberOfRowsInSection and you should be good to go.

Upvotes: 2

Related Questions