swiftuitableviewdidselectrowatindexpath

Did select show check/image using custom tableview cell


Ok so this seems like a really simple question, but I can't seem to solve it efficiently or find anything on the web that has the correct answer. So here goes.

I have a tableViewController with a prototype cell running off a custom class. Inside the prototype cell I have an image of a checkmark which I want to display when the user selects the cell and hide the image when another cell gets selected. I've got an approach working but it requires me to reloadData on the table which seems really inefficient. So there must be a better way right?

My code...

//CUSTOM CLASS (trimmed code down to just show relevant code)
class GoalsTableViewCell: UITableViewCell {

    @IBOutlet weak var gNameLabel: UILabel!
    @IBOutlet weak var gIsSelectedImage: UIImageView!
    
    }
}

//TABLE VIEW CONTROLLER (trimmed it down to just show relevant code)
class GoalsTableVC: UITableViewController {

    var selectedGoalId = ""
    var selectedInd = 0

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let gCell = tableView.dequeueReusableCell(withIdentifier: "gCell", for: indexPath) as! GoalsTableViewCell
        gCell.gNameLabel.text = goalsData[indexPath.row].gName
        if indexPath.row == selectedInd {
            gCell.gIsSelectedImage.isHidden = false
        } else {
            gCell.gIsSelectedImage.isHidden = true
        }

        return gCell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedGoalId = goalsData[indexPath.row].id!
        selectedInd = indexPath.row
        tableView.reloadData()
    }
}

Solution

  • It's possible to reload only the affected rows

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedGoalId = goalsData[indexPath.row].id!
        let indexPathsToReload = [indexPath, IndexPath(row: selectedInd, section: 0)]
        selectedInd = indexPath.row
        tableView.reloadRows(at: indexPathsToReload, with: .automatic)
    }