NSInternalInconsistencyException,在我关闭下一个场景

时间:2017-05-18 08:53:44

标签: ios swift xcode

  

由于未捕获的异常'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)
    }

应用程序崩溃,我该怎么办?

1 个答案:

答案 0 :(得分:0)

@ 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.
    }


}