2017-05-18 3 views
0

キャッチされない例外 'NSInternalInconsistencyException'が原因でアプリケーションを終了しています。理由: '無効な更新:セクション0の行数が無効です。 (1)は、更新(10)前にそのセクションに含まれる行の数に、そのセクション(挿入された1個、削除された0個)に挿入または削除された行の数をプラスまたはマイナスし、 (0は動かされ、0は動かされた)。NSInternalInconsistencyException、次のシーンを閉じた後にクラッシュする

私のアプリは、このページにクラッシュさ

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) 
    } 

でアプリがクラッシュを戻ってきた場合、私は何ができるのでしょうか? Paulw11 @

+0

例外メッセージはかなり明確です。あなたのテーブルには10のアイテムがありました。あなたは1つを挿入しましたが、 'numberOfRowsInSection'は11の代わりに1を返しました。コードを表示してください。あなたの 'insertRowMode3'は不必要に複雑に見えるし、' asyncAfter'は再帰がなくてもコードのにおいです。再帰ではそれは悪化します。 – Paulw11

+0

私はそれを調整するために何ができるのですか? @ Paulw11 – bero

+0

何が起こっているのかを見るのに十分なコードが表示されていませんが、コードをデバッグする必要があります。たとえば、テーブルビューに行を挿入しているようですが、通常はテーブルビューにデータを提供する配列ではありません。 – Paulw11

答えて

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


} 
+0

これは答えではありません。コードをそこに含めるには、あなたの質問を編集する必要があります(http://stackoverflow.com/posts/44043041/edit)。 – Paulw11

+0

しました@ Paulw11 – bero

関連する問題