I have a custom tableviewHeaderFooterView where I set up a target event for the button in the custom tableViewCell class (checkButton is the button and its background image changes to a checkmark when a user clicks on it).
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let userModel = Data.userModels[section]
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId") as! SectionHeader
cell.setup(model: userModel)
cell.checkButton.tag = section
cell.checkButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
return cell.contentView
}
And in that function I want to create or remove items from an array depending on whether the user taps on a cell or not (i.e. if they tap on the button, then add something to the array, but if they tap on that button again, then remove that object from the array.)
@objc func handleTap(sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected == true {
let model = ItemModel(itemName: item, price: price)
ItemModelFunctions.createItem(for: sender.tag, using: model)
}
if sender.isSelected == false {
ItemModelFunctions.removeFromUser(from: sender.tag)
}
print(sender.tag)
}
Here are the createItem and removeFromUser functions:
struct ItemModelFunctions {
static func createItem(for userIndex: Int, using itemModel: ItemModel) {
Data.userModels[userIndex].itemModels.append(itemModel)
}
static func removeFromUser(from userIndex: Int) {
Data.itemModels.remove(at: userIndex)
}
}
When I tap on the button twice to remove it from the array, I get an error saying Data.itemModels.remove(at: userIndex) is out of range.
I know using a prototype cell for a tableViewHeaderFooterView isn't exactly the correct way, but I've seen other programmers and YouTubers do this with success. Are my issues coming from using a prototype cell? Or am I removing the item from the array in the wrong way? Thank you all for your help!