tableView.insertRows抛出NSException,但不是``[0,0]]时的`insertRows`

时间:2017-08-25 22:34:44

标签: ios swift

我正在遵循本教程的修改(简化)版本,非常类似于此处的内容:

https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementNavigation.html#//apple_ref/doc/uid/TP40015214-CH16-SW1

这是我的UITableViewController

import UIKit

class NewsTableViewController: UITableViewController {

    @IBOutlet var newsTableView: UITableView!

    /*
        MARK: properties
    */
    var news = NewsData()

    override func viewDidLoad() {
        super.viewDidLoad()
        dummyNewData()
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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


    // here we communicate with parts of the app that owns the data
    override func tableView(_ tableView: UITableView
                           , cellForRowAt indexPath: IndexPath
                           ) -> UITableViewCell {

        // note here we're using the native cell class
        let cell = tableView.dequeueReusableCell(withIdentifier: "newsCell", for: indexPath)
        // Configure the cell...
        let row : Int = indexPath.row
        cell.textLabel?.text = news.read(idx: row)
        return cell
    }


    // MARK: Navigation ****************************************************************

    // accept message from CreateNewViewController
    @IBAction func unwindToCreateNewView(sender: UIStoryboardSegue){

        if let srcViewController = sender.source as? CreateNewsViewController
        , let msg = srcViewController.message {

            // push into news instance and display on table
            news.write(msg: msg)
            let idxPath = IndexPath(row: news.length(), section: 1)

            // tableView.insertRows(at: [idxPath], with: .automatic)
            tableView.insertRows(at: [[0,0]], with: .automatic)

            print("unwound with message: ", msg, idxPath)
            print("news now has n pieces of news: ", news.length())
            print("the last news is: ", news.peek())

        }

    }


    /*
        @DEBUG: debugging functions that display things on screen **************************
    */
    // push some values into new data
    private func dummyNewData(){

        print("dummyNewData")
        news.write(msg: "hello world first message")
        news.write(msg: "hello world second message")
        news.write(msg: "hello world third message")
    }

}

问题出在函数unwindToCreateNewView

let idxPath = IndexPath(row: news.length(), section: 1)
tableView.insertRows(at: [idxPath], with: .automatic)

其中news.length()为我提供的Int基本上是someArray.count

当我insertRows(at: [idxPath] ...)时,我收到错误:

libc++abi.dylib: terminating with uncaught exception of type NSException

但是当我只是硬编码时:

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

它运作得很好。在模拟器上,我看到新消息插入到前面的消息之下。是什么给了什么?

1 个答案:

答案 0 :(得分:2)

以下代码存在“一个一个”的问题:

news.write(msg: msg)
let idxPath = IndexPath(row: news.length(), section: 1)

假设在调用此代码之前,news中没有项目。这意味着有0行。如果要添加新行,则需要在第0行插入,因为行号从0开始。

调用news.write(msg: msg)添加新项目,其长度现为1。

调用IndexPath(row: news.length(), section: 1)将行设置为值1,但它必须为0.

一个简单的解决方案是交换这两行:

let idxPath = IndexPath(row: news.length(), section: 1)
news.write(msg: msg)

这将创建具有正确行号的索引路径。

由于这是第一个(也是唯一的)部分,除了上述更改外,还需要将部分编号更改为0.

let idxPath = IndexPath(row: news.length(), section: 0)
news.write(msg: msg)