Ich arbeite an meinem TableView und gerade jetzt habe ich die Funktion zum Löschen der Zeilen implementiert, aber nach dem Löschen einer Zeile stürzt die App mit diesem Fehler ab (schwerwiegender Fehler: Index außerhalb des Bereichs)Swift Fehler: Index außerhalb des Bereichs
struct User {
var name: String
var images: UIImage
var coordinate : (Double , Double)
var type: String
var address: String
}
var users = [User]()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
rows = 0
tableView.reloadData()
insertRowsMode3()
}
@IBAction func refreshTapped(_ sender: Any) {
rows = 0
tableView.reloadData()
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.20) {
self.insertRowMode3(ind: ind+1)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
let user = users[indexPath.row]
cell.selectionStyle = .none
cell.myImage.image = user.images
cell.myLabel.text = user.name
cell.myTypeLabel.text = user.type
return (cell)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
UIView.animate(withDuration: 0.2, animations: {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
})
performSegue(withIdentifier: "goToLast" , sender: users[indexPath.row])
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
users.remove(at: indexPath.row)
tableView.reloadData()
}
}
dies der Teil des Codes meiner Tableview ist, ich glaube, ich weiß, warum die Anwendung in Absturz gehen, aber ich weiß nicht, wie dieses Problem zu lösen, was kann ich tun?
hallo, in dieser Linie, wo die var Mitglieder gaben kommen aus users.remove (at: indexPath.row)? –
In 'numberOfRowsInSection' statt' Rückkehr rows' Rückkehr 'return users.count'. Manuelle Berechnung der Anzahl der Zeilen ist ziemlich fehleranfällig. – vadian
@ S.Wei richtig! Ich habe die Frage bearbeitet – bero