有些日子我正在尝试调整一个问题,导致我的应用在解雇期间崩溃,我的代码就是这个
import UIKit
import MapKit
import CoreLocation
class CourseClass: UITableViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
struct User {
var name: String
var images: UIImage
var coordinate: (Double, Double)
var type: String
var address: String
}
var rows = 0
var users = [User]()
let CS = User(name: "Sporting Club", images: UIImage(named: "Caffè Spagar.png")! , coordinate:(12345,54678), type: "Sport", address: "Via dei Cocchieri, 1A")
let D = User(name: "94Tele", images: UIImage(named: "Duks.png")! , coordinate:(13345,54128), type: "Restaurant", address: "Via della Madonna dei Monti, 94")
let PS = User(name: "Chiostro del Bramante ", images: UIImage(named: "Posta station.png")! , coordinate:(18795,34556), type: "Museum", address: "Arco della Pace, 5")
let B = User(name: "Teatro Sala Umberto", images: UIImage(named: "Barnum.png")! , coordinate:(46655,43554), type: "Theater", address: "Via della Mercede, 50")
let EC = User(name: "Mangiarte", images: UIImage(named: "Elephant Club.png")! , coordinate:(12325,21435), type: "Restaurant", address: "Via degli Equi, 16")
override func viewDidLoad() {
super.viewDidLoad()
map.showsUserLocation = true
map.delegate = self
users.append(CS)
users.append(D)
users.append(PS)
users.append(B)
users.append(EC)
}
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)
}
}
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
let user = users[indexPath.row]
cell.myImage.image = user.images
cell.myLabel.text = user.name
cell.myTypeLabel.text = user.type
return (cell)
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToLast" , sender: users[indexPath.row])
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
@IBAction func mapTapped(_ sender: UITapGestureRecognizer) {
let latitude:CLLocationDegrees = 41.8919300
let longitude:CLLocationDegrees = 12.5113300
let regionDistance:CLLocationDistance = 1000;
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)]
let placemark = MKPlacemark(coordinate: coordinates)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "My House"
mapItem.openInMaps(launchOptions: options)
}
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
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
所以当我进入另一个场景时,一切都还可以,但是当我想回来时
@IBAction func lastBack(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
我的应用因此而崩溃
“因未捕获的异常而终止应用程序'NSInternalInconsistencyException',原因:'无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(1)必须等于更新前的该部分中包含的行(5),加上或减去从该部分插入或删除的行数(插入1个,删除0个)以及加入或减去移入或移出该部分的行数(0移动) in,0搬出去。'
我真的不知道如何调整代码以使其正常工作。
答案 0 :(得分:0)
错误本身就说问题了。与rows
数据存在一些不一致。
正如您所看到的,当您第一次访问此CourseClass
控制器时,您正在通过调用insertRowsMode3
中的viewDidAppear
来设置行。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
insertRowsMode3()
}
问题是,当您返回时,row
数据与您使用zero
初始化的数据不同,并且viewDidAppear
未被调用。我建议避免将data
放入或查看控制器,它会违反MVC
的核心架构,包括错误位置的数据层。