終止應用程序後,我駁回由於未捕獲的異常「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)
}
應用程序崩潰,我該怎麼辦?
異常消息是相當清楚的;你的桌子上有10件物品。你插入了一個,但你的'numberOfRowsInSection'返回1而不是11.顯示那個代碼。你的'insertRowMode3'看起來不必要的複雜,即使沒有遞歸,'asyncAfter'也是一種代碼味道。遞歸它更糟糕。 – Paulw11
所以我可以做些什麼來調整它? @ Paulw11 – bero
你沒有看到足夠的代碼來看看發生了什麼,但你需要調試你的代碼。例如,你似乎在tableview中插入一行,而不是在一個數組中,這通常是什麼將數據提供給tableview – Paulw11