A general comment first. You could streamline your code by replacing statement like
if unlocked.contains("Summer") {
} else {
unlocked.append("Summer")
}
by
if !unlocked.contains("Summer") {
unlocked.append("Summer")
}
Which line do you get the crash exactly ? (code is slightly different from the snippet you present, so that makes it difficult to localise where this occurs); is it in didSelectRowAt ? Line 7, 10 or 16, 19 ?
If that's here: i don't understand how you use names: rows in sections will be 0, 1, 2, restarting at zero in each section
you call self.unlocked[indexPath.row] ; Could you test with a print the size of self.unlocked and the number of rows in section ?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row)
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.section == 0 {
let icon = self.unlocked[indexPath.row]
setIcon(name: icon)
if IconChange.setIconPurchased == true {
iconSelected = names[indexPath.row]
UserDefaults.standard.setValue(names[indexPath.row], forKey: "iconSelected")
IconSelector.reloadData()
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
} else if indexPath.section == 1 {
let icon = self.locked[indexPath.row]
setIcon(name: icon)
if IconChange.setIconPurchased == true {
iconSelected = names[indexPath.row]
UserDefaults.standard.setValue(names[indexPath.row], forKey: "iconSelected")
IconSelector.reloadData()
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
IconChange.pushBack = true
self.navigationController?.popViewController(animated: true)
}
}
}
Yet another comment.
Maintaining several arrays to keep track of what is in tableView is really a risky design (the crash is just an illustration).
You'd better have a single dataSource, with attributes as locked: Bool instead of locked and unlocked arrays.