Shivam Tripathi
Shivam Tripathi

Reputation: 1493

Table View Cell getting deselected after first tap.

enter image description here

Gif depicting that :

When user selecting table view cell for first time(checkbox ticked first time) , cell getting selected but after that it is deselected automatically and nothing happens when i am tapping second time. But when i am tapping third time cell getting selected properly and on 4th tap it is deselecting properly and so on for 5th , 6th time onwards.

My didSelectRowAt() method looks like this :

func expandableTableView(_ expandableTableView: LUExpandableTableView, didSelectRowAt indexPath: IndexPath) {

    let cell = expandableTableView.cellForRow(at: indexPath) as! FilterTableCell

    let dictKey : String = FilterKeysMapping[FilterKeysFront.object(at: indexPath.section) as! String]!

    if(self.FilterDictAPI[dictKey] == nil){
        self.FilterDictAPI[dictKey] = [indexPath.row: self.FilterValueArray.object(at: indexPath.row)]
    }
    else{
        self.FilterDictAPI[dictKey]![indexPath.row] = self.FilterValueArray.object(at: indexPath.row)
    }

    self.expandableTableView.beginUpdates()
    cell.button.isSelected = true
    self.expandableTableView.reloadRows(at: [indexPath], with: .automatic)
    self.expandableTableView.endUpdates()
    expandableTableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}

didDeselectRowAt() method is like this :

   func expandableTableView(_ expandableTableView: LUExpandableTableView, didDeselectRowAt indexPath: IndexPath) {

        print("Did Deselect Cell at section \(indexPath.section) row \(indexPath.row)")
        let cell = expandableTableView.cellForRow(at: indexPath) as! FilterTableCell
        cell.button.isSelected = false        
        let dictKey : String = FilterKeysMapping[FilterKeysFront.object(at: indexPath.section) as! String]!

        if(self.FilterDictAPI[dictKey] != nil){
            self.FilterDictAPI[dictKey]?.removeValue(forKey: indexPath.row)
        }
        print("dict after removing values : \(self.FilterDictAPI)")
    }

cellForRowAt() method is :

   func expandableTableView(_ expandableTableView: LUExpandableTableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        guard let cell = expandableTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as? FilterTableCell else {
            assertionFailure("Cell shouldn't be nil")
            return UITableViewCell()
        }
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.label.text = "\(self.FilterValueArray.object(at: indexPath.row))" + "  (" + "\(self.FilterCountArray.object(at: indexPath.row))" + ")"
        return cell
    }

Table View Cell is :

class FilterTableCell: UITableViewCell {

    let label = UILabel()
    let button = UIButton()
    var check = Bool()

    // MARK: - Init

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(label)
        contentView.addSubview(button)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - Base Class Overrides

    override func layoutSubviews() {
        super.layoutSubviews()

        label.frame = CGRect(x: 42, y: 0, width: contentView.frame.width-42, height: contentView.frame.height)
        self.label.font = UIFont(name: "PlayfairDisplay-Regular", size: 18)
        button.frame = CGRect(x:10, y: contentView.frame.height/2-8, width: 16, height: 16)
        button.setImage(UIImage(named: "CheckboxUnchecked"), for: .normal)
        button.setImage(UIImage(named: "CheckboxChecked"), for: .selected)
        button.setImage(UIImage(named: "CheckboxUnchecked"), for: .highlighted)
    }

}

Issue as mentioned is only this : After first tap it is deselecting automatically.

Upvotes: 1

Views: 146

Answers (1)

Aakash
Aakash

Reputation: 2269

What's exactly happening is in didSelectRowAt when you reload that indexPath, that cell is deselected automatically and didDeselectRowAt method is called where cell.button.isSelected = false removes the checkmark.

So, to fix this comment out the below lines in didSelectRowAt method.

self.expandableTableView.beginUpdates()
self.expandableTableView.reloadRows(at: [indexPath], with: .automatic)
self.expandableTableView.endUpdates()

Also, reset button's selected state in cell's prepareForReuse() method. This will fix the undefined behaviour where checkbox is selected randomly or after first or second taps.

override func prepareForReuse() {
    super.prepareForReuse()

    button.isSelected = false
}

Upvotes: 2

Related Questions