Приложение вылетает, когда в UITableview очень быстро удаляет строку и возвращается к предыдущему виду - PullRequest
0 голосов
/ 27 сентября 2018

У меня есть 2 простых представления (ViewController и TableviewController), из первого представления показывают второе представление.Во втором представлении (Tableview) отображается список массивов.Когда я очень быстро удаляю строку в Tableview и сразу же возвращаюсь назад (первый вид), приложение вылетает с ошибкой:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x1d6bc7ef8 0x1d5d95a40 0x1d6b3d85c 0x1d6aace20 0x203afaa44 0x203acf4f8 0x203acf694 0x203aea3f4 0x203afa900 0x203c6ba34 0x203afa840 0x203af8dbc 0x20357a544 0x203580da4 0x2035811dc 0x203581e5c 0x20402c3c8 0x203c6ce68 0x203c42ee4 0x203c434d0 0x1db1bb394 0x104a84de4 0x104a92a94 0x1d6b561bc 0x1d6b51084 0x1d6b505b8 0x1d8dc4584 0x2035cb558 0x102452290 0x1d6610b94)
libc++abi.dylib: terminating with uncaught exception of type NSException

Gif screenshot

Мой код:

import UIKit

class TestTableViewController: UITableViewController {

    var items = ["Apple", "Samsung", "Xiaomi", "Huawei", "Oppo", "Vivo"]

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

    // MARK: - Table view data source

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

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

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

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            items.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }
}

Но, если я удаляю строку и жду некоторое время (0,4-1 сек) или перезагружаю табличное представление после удаления, никаких сбоев не происходит.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        items.remove(at: indexPath.row)
        tableView.reloadData() //After adding this line didn't handle any crashing
        //tableView.deleteRows(at: [indexPath], with: .fade)
    }
}

1 Ответ

0 голосов
/ 27 сентября 2018

Попробуйте:

            self.tableView.beginUpdates()
            items.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
            self.tableView.endUpdates()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...