I'm trying to remove an item of an array when the user deselects a cell. I understand why my code it's not working, basically, the array might contain 5 elements and if there are 100 cells and the user selected cell 10, trying to deselect it via indexPath.row would crash since the array has only 5 elements. The point is when the user deselect the cell, it should remove the corresponding element from the array. That's what I'm not sure how to do
var transferUsers = [UserModel]()
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
selectedUsersCount -= 1
if selectedUsersCount == 0 {
nextButton.isEnabled = false
}
cell.accessoryType = .none
transferUsers.remove(at: indexPath.row)
}
}
I tried this answer, but I get an error saying:
Cannot invoke 'index' with an argument list of type '(of: UserModel)'
UserModeldoes not conform toEquatable.