【问题标题】:NSInternalInconsistencyException, crashing after i dismiss the next sceneNSInternalInconsistencyException,在我关闭下一个场景后崩溃
【发布时间】:2017-05-18 19:39:29
【问题描述】:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新(1)后现有节中包含的行数必须等于行数更新前包含在该节中的行数 (10),加上或减去从该节插入或删除的行数(1 插入,0 删除),加上或减去移入或移出该节的行数(0 移入, 0 移出)。'

我的应用在这个页面崩溃了

import UIKit
import MapKit
import CoreLocation

struct place {



}



class CourseClass: UITableViewController, CLLocationManagerDelegate, MKMapViewDelegate {


    @IBOutlet weak var map: MKMapView!



    var place = ["Caffè Spagar", "Duks", "Posta station", "Barnum", "Elephant Club", "Cinema", "Space", "Andromeda", "Rodolf", "Devil Chair"]
    var rows = 0





    override func viewDidLoad() {
        super.viewDidLoad()

      map.showsUserLocation = true
      map.delegate = self
    }



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

        insertRowsMode3()
    }


    func insertRowsMode2() {

        for i in 0..<place.count {
            insertRowMode2(ind: i, str: place[i])
        }

    }

    func insertRowMode2(ind:Int,str:String) {

        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 < place.count-1 else { return }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.15) {

            self.insertRowMode3(ind: ind+1)
        }
    }











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

        return 1
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return rows
    }



    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

        cell.myImage.image = UIImage(named: (place[indexPath.row] + ".png"))
        cell.myLabel.text = place[indexPath.row]

        return (cell)
    }





    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       performSegue(withIdentifier: "goToLast" , sender: place[indexPath.row])

    }


    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }





    @IBAction func mapTapped(_ sender: Any) {

    let userLocation = map.userLocation

    let region = MKCoordinateRegionMakeWithDistance((userLocation.location?.coordinate)!,2000,2000)

    map.setRegion(region, animated: true)
    }







    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

        guest.local = sender as! String

        guest.localImage = UIImage(named: (sender as! String) + ".png")!

        }
    }


override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

在我关闭下一个 UIViewController 之后;所以当我从前一个场景进入页面时,没关系,但是如果我在之后的场景中继续,然后返回

 @IBAction func lastBack(_ sender: Any) {

    dismiss(animated: true, completion: nil)
    }

应用崩溃了,我该怎么办?

【问题讨论】:

  • 异常信息很清楚;你的桌子有 10 个项目。您插入了一个,但您的 numberOfRowsInSection 返回 1 而不是 11。显示该代码。您的insertRowMode3 看起来不必要地复杂,asyncAfter 即使没有递归也是代码味道。使用递归,情况会更糟。
  • 那么我能做些什么来调整它? @Paulw11
  • 您没有显示足够的代码来查看发生了什么,但您需要调试您的代码。例如,您似乎在 tableview 中插入一行,而不是在数组中,这通常是向 tableview 提供数据的原因
  • @Paulw11 我现在发布了整个代码
  • 你应该在谷歌上搜索一些 UITableView 教程或阅读 Apple 的表格视图编程指南;驱动 tableview 的规范方法是有一个支持数组。所有这些insertRows 的东西和增加row 变量都行不通。

标签: ios swift xcode


【解决方案1】:

@Paulw11 这是整个代码

import UIKit
import MapKit
import CoreLocation

struct place {



}



class CourseClass: UITableViewController, CLLocationManagerDelegate, MKMapViewDelegate {


    @IBOutlet weak var map: MKMapView!



    var place = ["Caffè Spagar", "Duks", "Posta station", "Barnum", "Elephant Club", "Cinema", "Space", "Andromeda", "Rodolf", "Devil Chair"]
    var rows = 0





    override func viewDidLoad() {
        super.viewDidLoad()

      map.showsUserLocation = true
      map.delegate = self
    }



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

        insertRowsMode3()
    }


    func insertRowsMode2() {

        for i in 0..<place.count {
            insertRowMode2(ind: i, str: place[i])
        }

    }

    func insertRowMode2(ind:Int,str:String) {

        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 < place.count-1 else { return }
        DispatchQueue.main.asyncAfter(deadline: .now()+0.15) {

            self.insertRowMode3(ind: ind+1)
        }
    }











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

        return 1
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return rows
    }



    public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

        cell.myImage.image = UIImage(named: (place[indexPath.row] + ".png"))
        cell.myLabel.text = place[indexPath.row]

        return (cell)
    }





    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       performSegue(withIdentifier: "goToLast" , sender: place[indexPath.row])

    }


    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }





    @IBAction func mapTapped(_ sender: Any) {

    let userLocation = map.userLocation

    let region = MKCoordinateRegionMakeWithDistance((userLocation.location?.coordinate)!,2000,2000)

    map.setRegion(region, animated: true)
    }







    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

        guest.local = sender as! String

        guest.localImage = UIImage(named: (sender as! String) + ".png")!

        }
    }







    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

【讨论】:

  • 这不是答案;您应该 edit 您的问题在其中包含代码。
  • 做到了@Paulw11
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多