checkmark in tableView is disappear

Hi!


When I Scrolling the TableView, the checkmark has been disappear or appear in other index.


Someone can help me? Some tip?

Please!



Cheers.


The code...



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


if let cell = self.myTableViewPer.cellForRow(at: indexPath as IndexPath) {

if cell.accessoryType == .checkmark{

cell.accessoryType = .none

//my logic here...


}else if cell.accessoryType == .none{

cell.accessoryType = .checkmark


//other logic here...

}

}


//my tableView and Cells have 11 index.

Answered by OOPer in 345493022

It is not strange if you know that a cell is just a temporary view of an item in your data model.

If you want to display selection state as `accesoryType`, you need to set accessoryType in `tableView(_:cellForRowAt:)`.


Simplified example:

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.allowsMultipleSelection = true
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = String(indexPath.row) //Or other setups for your cell
        cell.selectionStyle = .none
        if
            let selectedRows = tableView.indexPathsForSelectedRows,
            selectedRows.contains(indexPath)
        {
            cell.accessoryType = .checkmark

        } else {
            cell.accessoryType = .none
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = self.tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .checkmark
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = self.tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .none
    }


Or UITableView can show nice checkmarks in editing mode.

Accepted Answer

It is not strange if you know that a cell is just a temporary view of an item in your data model.

If you want to display selection state as `accesoryType`, you need to set accessoryType in `tableView(_:cellForRowAt:)`.


Simplified example:

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.allowsMultipleSelection = true
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = String(indexPath.row) //Or other setups for your cell
        cell.selectionStyle = .none
        if
            let selectedRows = tableView.indexPathsForSelectedRows,
            selectedRows.contains(indexPath)
        {
            cell.accessoryType = .checkmark

        } else {
            cell.accessoryType = .none
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = self.tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .checkmark
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        let cell = self.tableView.cellForRow(at: indexPath)
        cell?.accessoryType = .none
    }


Or UITableView can show nice checkmarks in editing mode.

Thank you. You help me to much.


I just remove:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") at viewDidLoad becouse I have de customCell class and than works fine now.



Cheers.

checkmark in tableView is disappear
 
 
Q