DannyBoi8181
DannyBoi8181

Reputation: 33

TableView didSelectRowAt IndexPath not getting called

Im currently setting up a grouped table view which have check boxes set on a tableview cell in a section when the view loads depending on results pulled from user defaults. When the user taps on a different cell in the section the checkbox on the other cell will disappear and a new checkbox will show on the cell they just tapped.

This seems to work when tapping on the different cells initially but after 3 changes you then have to tap on the cell twice for the checkboxes to change. It would seem like every other tap tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) is not getting called.

Thanks for any help in advance, code below :

var selectedIndex: IndexPath? = nil

let usersettings = UserDefaults.standard

override func viewDidLoad() {
    super.viewDidLoad()
    settingsTableView.delegate = self
    settingsTableView.dataSource = self
    //Load current preferences from user defaults
    spamNumbersSetting = usersettings.value(forKey: "spamNumbersSetting") as! Int
    personalNumbersSetting = usersettings.value(forKey: "personalNumbersSetting") as! Int
    settingsSaveButton.isEnabled = false
    // Do any additional setup after loading the view.
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.white
}

func numberOfSections(in tableView: UITableView) -> Int {
    return settingsHeaders.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return settingsHeaders[section]
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return settingsContent[section].count
}

func tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell {

    let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
    //Preset ticks depending on value of user settings
    if indexPath.section == 0 {
        if (spamNumbersSetting == 1) {
            if indexPath.row == 0 {
                cell.accessoryType = UITableViewCellAccessoryType.checkmark
                selectedIndex = indexPath
            }
        } else {
            if indexPath.row == 1 {
                cell.accessoryType = UITableViewCellAccessoryType.checkmark
                selectedIndex = indexPath
            }
        }
    }

    if indexPath.section == 1 {
        if (personalNumbersSetting == 1){
            cell.accessoryType = UITableViewCellAccessoryType.checkmark
        }
    }

    cell.textLabel?.textColor = UIColor.white
    let section = indexPath.section
    cell.textLabel?.text = settingsContent[section][indexPath.row]


    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.tableView(tableView, didDeselectRowAt: selectedIndex!)
    settingsSaveButton.isEnabled = true
    let newCell = tableView.cellForRow(at: indexPath)
    print("\(newCell?.textLabel?.text) is now the active cell !")
    let oldCell = tableView.cellForRow(at: selectedIndex!)
    //Deselect old cell
    if indexPath.section == 0 {
        if newCell?.accessoryType != .checkmark {
            newCell?.accessoryType = .checkmark
            //oldCell?.accessoryType = .none
        } else {
            newCell?.accessoryType = .none
        }

        selectedIndex = indexPath
    }

}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    print("Deselected \(cell?.textLabel?.text) !")
    cell?.accessoryType = .none
}

Upvotes: 1

Views: 644

Answers (1)

Mamun
Mamun

Reputation: 131

Deselect action perform into didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView .deselectRow(at: indexPath, animated: false)

}

Upvotes: 1

Related Questions