【问题标题】:App crush: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0应用程序崩溃:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效
【发布时间】:2017-05-26 10:52:09
【问题描述】:
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 的代码,我添加了一个带有特定动画的 tableView,但是当我更改视图并返回 dismiss(animated: true, completion: nil)应用程序崩溃时,因为存在一些不一致与行数据。如您所见,当我第一次访问此CourseClass2 控制器时,我通过在viewDidAppear 中调用insertRowsMode3 来设置行。

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

    insertRowsMode3()
}

问题是当我回来时,行数据与我用零初始化的不同,并且 viewDidAppear 没有被调用。 我知道错误,但我不知道如何更改代码以使其正常工作。我真的需要帮助。

应用完全在这个函数中迷恋

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)

【问题讨论】:

  • 请对您发布的代码进行一些审查。它就像200行。只留下我们需要查看的代码。谢谢
  • 放置断点并检查遇到此错误的位置。
  • 编辑@RinkiNegi

标签: ios swift xcode


【解决方案1】:

您在第 0 节中提到了无效的行数: 在 insertRowMode3 中,您插入行但不更新表视图。 因此,即使在更新后,您的表格视图在表格视图中的行数也相同,这就是不一致的地方。

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

如果对您没有帮助,请告诉我。

【讨论】:

  • 我必须在哪里添加 view.beginUpdates() ?你能给我举个例子吗?
  • 在此之前:tableView.insertRows(at: [indPath], with: .right)
  • 你可以参考这个:self.tableViewSlotsDay.beginUpdates() self.AddCountSection1 = self.AddCountSection1 + 1 let indexPath = IndexPath(item: self.AddCountSection1 - 1, section: 0) self.tableViewSlotsDay. insertRows(at: [indexPath], with: .fade) self.tableViewSlotsDay.endUpdates()
  • 好的理解,只有 'AddcountSection' 我不知道我要在这个函数中插入什么来创建它
  • 不,那只是行数(tableview 中的行数)。我根据我的需要通过这个变量来维护它。您正在使用“行变量”进行维护,只需替换这些词。
猜你喜欢
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 1970-01-01
  • 2012-07-28
相关资源
最近更新 更多