Пожалуйста, смотрите ниже код.Вам нужно установить accessoryType по умолчанию в ячейке для Row, а затем в didSelectRow вы можете изменить его в соответствии с вашей логикой.Я создал простую модель, чтобы показать, как вы можете установить accessoryType по умолчанию при перезагрузке
import UIKit
struct CellInfo{
var title:String
var isSelected:Bool
}
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTV: UITableView!
var CellInfoArr = [
CellInfo(title: "First Row", isSelected: false),
CellInfo(title: "Second Row", isSelected: false),
CellInfo(title: "Third Row", isSelected: false),
CellInfo(title: "Fourth Row", isSelected: false),
CellInfo(title: "Fifth Row", isSelected: false)
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myTV.delegate = self
myTV.dataSource = self
myTV.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return CellInfoArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = CellInfoArr[indexPath.row].title
if CellInfoArr[indexPath.row].isSelected == true{
cell.accessoryType = .checkmark
}else{
cell.accessoryType = .none
}
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
CellInfoArr[indexPath.row].isSelected = false
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
CellInfoArr[indexPath.row].isSelected = true
}
}
}