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.
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.