i have a table view in which i have two sections. Both sections contain 3 cells under it. Now when i select any cell it shows tick sign, but it selects one cell from both section. i have tried some code but it isn't working. I want that user can select a single cell from both sections and when the table view load its first cell should be preselected. How can i do that? i'm bit confused about preselection of cell.My code for cell selection is this,
self.filterTableView.allowsSelection = true
extension FiltersVC: UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems[section].count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = filterTableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FiltersTableViewCell
cell.tilteLbl.text = menuItems[indexPath.section][indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = #colorLiteral(red: 0.9130856497, green: 0.9221261017, blue: 0.9221261017, alpha: 1)
let headerText = UILabel()
headerText.textColor = UIColor.black
headerText.adjustsFontSizeToFitWidth = true
switch section{
case 0:
headerText.textAlignment = .center
headerText.text = "LIST BY"
headerText.backgroundColor = #colorLiteral(red: 0.9190355449, green: 0.9281349067, blue: 0.9281349067, alpha: 1)
headerText.font = UIFont.boldSystemFont(ofSize: 20)
case 1:
headerText.textAlignment = .center
headerText.text = "COUSINE"
headerText.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
headerText.font = UIFont.boldSystemFont(ofSize: 20)
default: break
}
return headerText
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if let cell = filterTableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "listBy")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
}
else if indexPath.section == 1{
if let cell = filterTableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "Cuisine")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .none
}
}
else if indexPath.section == 1{
if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .none
}
}
}