无法将行插入表“试图将第0行插入第0节,但更新后只有0个节”

时间:2019-05-05 20:32:23

标签: ios swift uitableview

按照本教程https://www.hackingwithswift.com/read/5/overview

当我在表格顶部插入一行时,我总是收到此错误,

  

'NSInternalInconsistencyException',原因:“试图将第0行插入第0节,但更新后只有0个节”“

这是处理此问题的代码。

func submit(_ answer: String) {
    let lowerAnswer = answer.lowercased()

    if isPossible(word: lowerAnswer) {
        if isOriginal(word: lowerAnswer) {
            if isReal(word: lowerAnswer) {
                usedwords.insert(answer, at: 0)

                //the below code is for animation even though we just inserted at index 0, the top of the table!

                let indexPath = IndexPath(row: 0, section: 0)

                tableView.insertRows(at: [indexPath], with: .automatic)


            }
        }
    }
}

应该允许我从UIAlertController插入数据,然后在顶部填充表格视图。

下面是我的ViewController

class ViewController: UITableViewController {
    var allwords = [String]()
    var usedwords = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        if let startWordsURL = Bundle.main.url(forResource: "start", withExtension: "txt") {
            if let startWords = try? String(contentsOf: startWordsURL) {
                allwords = startWords.components(separatedBy: "\n")
            }
        }
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(promptForAnswer))

        if allwords.isEmpty {
            allwords = ["silkworm"]
            }

        startGame()
    }

    func startGame() {
        title = allwords.randomElement()
        usedwords.removeAll(keepingCapacity: true)
        tableView.reloadData()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return usedwords.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Word", for: indexPath)
        cell.textLabel?.text = usedwords[indexPath.row]
        return cell
    }

    @objc func promptForAnswer() {
        let ac = UIAlertController(title: "Enter Answer", message: nil, preferredStyle: .alert)
        ac.addTextField()

        let submitAction = UIAlertAction(title: "Submit", style: .default) {
            [weak self, weak ac] action in
            guard let answer = ac?.textFields?[0].text else {return}
            self?.submit(answer)
        }

        ac.addAction(submitAction)
        present(ac, animated: true)
    }

    func submit(_ answer: String) {
        let lowerAnswer = answer.lowercased()

        if isPossible(word: lowerAnswer) {
            if isOriginal(word: lowerAnswer) {
                if isReal(word: lowerAnswer) {
                    usedwords.insert(answer, at: 0)

                    //the below code is for animation even though we just inserted at index 0, the top of the table!

                    let indexPath = IndexPath(row: 0, section: 0)

                    tableView.insertRows(at: [indexPath], with: .automatic)
                }
            }
        }
    }

    func isPossible(word: String) -> Bool {
        return true
    }

    func isOriginal(word: String) -> Bool {
        return true
    }

    func isReal(word: String) -> Bool {
        return true
    }
}

2 个答案:

答案 0 :(得分:0)

您将返回usedWords.count的节数,但尚未实现numberOfRowsInSection。这意味着您的表中有usedWords.count个节,每个节有0行。

您告诉tableview您正在插入一行,但是numberOfRowsInSection仍为0,因此会出现异常。

您想要一个包含usedWords.count行的单个部分:

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) {
    return usedWords.count
}

答案 1 :(得分:0)

下面的代码在xamarin.ios中,你可以用swift写。

  1. 先更新数据源
  2. 尝试添加新的零部分。
var sectionindex = new NSIndexSet(0);
tableView.InsertSections(sectionindex,UITableViewRowAnimation.Bottom);