静态TableView获取NSRangeException

问题描述 投票:1回答:2

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

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

这是控制台日志:

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

连接:

我究竟做错了什么?谢谢。

ios swift xcode sigabrt
2个回答
0
投票

您正在使用静态单元格。没有理由实现numberOfSectionsnumberOfRowsInSection,因为它们是由Storyboard中的静态布局指定的。

因为你已经实现了:

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

你告诉iOS每个部分都有3行,这是谎言。所以iOS尝试访问第一部分的第二行,并且数组索引超出范围崩溃,因为该部分只有1行。

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


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.