bero bero - 25 days ago 5
Swift Question

Swift error: index out of range

i'm working on my tableView and just now i implemented the function to delete the rows, but after i delete a row the app crash with this error (fatal error: index out of range)

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

}
}


this is the part of the code of my tableView, i think i know why the application go in crash but i don't know how to solve this problem, what i can do?

V12 V12
Answer Source

update your numberOfRowsInSectionas rather than return rows return return users.count as per @vadian's comment. and update delete code with this

users.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)