Вы можете попробовать следующий метод, чтобы сохранить одну ячейку выбранной одновременно
**My arrays**
let section = ["pizza", "deep dish pizza", "calzone"]
let items = [["Margarita", "BBQ Chicken", "Peproni", "BBQ Chicken", "Peproni"], ["sausage", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "prawns & mashrooms"]]
/// Lets keep index Reference for which cell is
/// Getting selected
var selectedIndex : [Int:Int]?
/// Now in didLoad
override func viewDidLoad() {
super.viewDidLoad()
/// Initialise the Dictionary
selectedIndex = [Int:Int]()
/// Set the Default value as in your case
/// Section - 0 and IndexPath - 0
/// i.e First cell
selectedIndex?.updateValue(0, forKey: 0)
mainTableView.delegate = self
mainTableView.dataSource = self
mainTableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return section.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
/// my Header cell
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! TableViewCell
headerCell.titleLabel.text = self.section[section]
headerCell.ButtonToShowHide.tag = section
return headerCell.contentView
}
/// Now in CellForRowMethod
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = items[indexPath.section][indexPath.row]
/// Compare if current section is Available in reference Dict
if let val = selectedIndex![indexPath.section]{
/// If Yes
/// Check IndexPath Row Value
if indexPath.row == val{
/// If row is found that is selected
/// Make it highlight
/// You can set A radio button
cell.textLabel?.textColor = UIColor.red
}
else{
/// Set default value for that section
cell.textLabel?.textColor = UIColor.black
}
}
/// If no
else{
/// Required to set Default value for all other section
/// And
/// Required to Update previous value if indexPath was selected
/// In previouus index Section
cell.textLabel?.textColor = UIColor.black
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
/// Remove All values
selectedIndex?.removeAll()
/// Insert current Value
selectedIndex?.updateValue(indexPath.row, forKey: indexPath.section)
/// Reload TableView Fully
/// Or you can keep Reference of Previous and New Value
/// For not reloading All cells
self.mainTableView.reloadData()
}
Выход
, когда TableView загружен
![enter image description here](https://i.stack.imgur.com/m4Itb.png)
При выборе строки в том же разделе
![enter image description here](https://i.stack.imgur.com/qyJYP.png)
при выборе строки в другом разделе
![enter image description here](https://i.stack.imgur.com/MriwL.png)
GIF Работа - Обновление цвета метки ячейки
![enter image description here](https://i.stack.imgur.com/biMCe.gif)
Повторное обновление
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = items[indexPath.section][indexPath.row]
/// Compare if current section is Available in reference Dict
if let val = selectedIndex![indexPath.section]{
/// If Yes
/// Check IndexPath Row Value
if indexPath.row == val{
/// If row is found that is selected
/// Make it highlight
/// You can set A radio button
cell.accessoryType = .checkmark
}
else{
/// Set default value for that section
cell.accessoryType = .none
}
}
/// If no
else{
/// Required to set Default value for all other section
/// And
/// Required to Update previous value if indexPath was selected
/// In previouus index Section
cell.accessoryType = .none
}
return cell
}