静态TableView获取NSRangeException

时间:2017-12-17 13:11:45

标签: ios swift xcode sigabrt

我有一个带有静态单元格的UITableViewController,当我运行应用程序并点击导致它的按钮时,我在AppDelegate中收到一个SIGABRT信号。

我试图找到未使用的插座,但它没有用。

这是控制台日志:

Console Log

UITableViewController代码:

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
    }
}

故事板中的UITableViewController

The UITableViewController in the Storyboard

连接:

The Connections

我做错了什么? 感谢。

2 个答案:

答案 0 :(得分:0)

阅读错误消息,请不要发布代码图片。

tableView:cellforRowAtIndexPath方法的某个地方,有一个空数组,但您正在尝试访问索引为1的元素。

答案 1 :(得分:0)

您正在使用静态单元格。没有理由实施if (preg_match('/^\d{10}$/', $string)) { //valid mobile } else if (filter_var($string, FILTER_VALIDATE_EMAIL)) { //valid email } else { //invalid } numberOfSections,因为这些是由故事板中的静态布局指定的。

因为您已实施:

numberOfRowsInSection

你告诉iOS每个部分都有override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 } 行,这是谎言。因此,iOS会尝试访问第一部分的第二行,并在数组索引超出范围时崩溃,因为该部分只有3行。

所以,删除1numberOfSections的实现,你应该好好去。