App crush:由于未捕获的exception“NSInternalInconsistencyException”而终止应用程序,原因是:“无效的更新:节0中的行数无效

import UIKit import MapKit import CoreLocation class CourseClass2: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var map: MKMapView! @IBOutlet weak var mapCourse: UIImageView! @IBOutlet weak var tableView: UITableView! struct User { var name: String var images: UIImage var coordinate: (Double, Double) var type: String var address: String } var rows = 0 override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) insertRowsMode3() } func insertRowsMode2() { for i in 0.. < users.count { insertRowMode2(ind: i, usr: users[i]) } } func insertRowMode2(ind: Int, usr: User) { let indPath = IndexPath(row: ind, section: 0) rows = ind + 1 tableView.insertRows(at: [indPath], with: .right) } func insertRowsMode3() { rows = 0 insertRowMode3(ind: 0) } func insertRowMode3(ind: Int) { let indPath = IndexPath(row: ind, section: 0) rows = ind + 1 tableView.insertRows(at: [indPath], with: .right) guard ind < users.count - 1 else { return } DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) { self.insertRowMode3(ind: ind + 1) } } func numberOfSections( in tableView: UITableView) - > Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) - > Int { return rows } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!MyTableViewCell let user = users[indexPath.row] cell.MyImage.image = user.images cell.MyLabel.text = user.name cell.MyTypeLabel.text = user.type return (cell) } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { performSegue(withIdentifier: "goToLast", sender: users[indexPath.row]) } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) - > CGFloat { return 100 } override func prepare( for segue: UIStoryboardSegue, sender: Any ? ) { if segue.identifier == "goToLast" { guard let vc = segue.destination as ? FinalClass else { return } let guest = segue.destination as!FinalClass if let user = sender as ? User { guest.local = user.name guest.localImage = user.images guest.localType = user.type guest.localAddress = user.address } } } @IBAction func IndTapped(_ sender: Any) { self.performSegue(withIdentifier: "goBack", sender: self) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

你好家伙,这是ViewController的代码,我得到的错误,我添加了一个特定的animationtableView,但当我改变的观点,比回来与dismiss(animated: true, completion: nil)的应用程序崩溃,因为与行数据有一些不一致。 正如你所看到的,当我第一次访问这个CourseClass2控制器时,我正在通过在viewDidAppear调用insertRowsMode3来设置行。

 override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) insertRowsMode3() } 

问题是,当我回来的行数据是不一样的,我已经初始化为零和viewDidAppear不会被调用。 我知道错误,但我不知道如何改变我的代码,使其工作。 我真的需要帮助。

该应用程序正好在这个function粉碎

 func insertRowMode3(ind:Int) { let indPath = IndexPath(row: ind, section: 0) rows = ind + 1 tableView.insertRows(at: [indPath], with: .right) guard ind < users.count-1 else { return } DispatchQueue.main.asyncAfter(deadline: .now()+0.15) { self.insertRowMode3(ind: ind+1) } } 

这里tableView.insertRows(at: [indPath], with: .right)

您在第0节中提到无效的行号:在insertRowMode3中,您插入行但不更新表视图。 所以你的表视图在表视图中获得相同数量的行甚至更新,这就是不一致的地方。

使用tableview.beginUpdates(),然后插入行,增加ur行的值,然后tableview.endUpdates()

让我知道,如果它不帮助你。