Static TableView gets NSRangeException

二次信任 提交于 2019-12-11 07:22:03

问题


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:

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 Connections:

What am I doing wrong? Thanks.


回答1:


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.




回答2:


Read the error message, and please do not post images of code.

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



来源:https://stackoverflow.com/questions/47855452/static-tableview-gets-nsrangeexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!