0

I have multiple section and each section can have multiple rows.

Code : Display as excepted.

 class SampleViewController: UIViewController {
    let sectionArray = ["pizza", "deep dish pizza", "calzone"]

    let items = [["Margarita", "BBQ Chicken", "Peproni"], ["Margarita", "meat lovers", "veggie lovers"], ["sausage", "chicken pesto", "BBQ Chicken"]]

    @IBOutlet weak var listObj: UITableView!

    var selectedItems = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        registerCell()
        // Do any additional setup after loading the view.
    }
    func registerCell(){
        self.listObj.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }


}
extension SampleViewController : UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionArray.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items[section].count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
         cell.textLabel?.text = items[indexPath.section][indexPath.row]
        if selectedItems.contains(items[indexPath.section][indexPath.row]) {
            print("Selected Item")
            cell.accessoryType = .checkmark
        } else {
            print("Item not selected")
            cell.accessoryType = .none
        }

         return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sectionArray[section].uppercased()
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedItems.append(items[indexPath.section][indexPath.row])
        tableView.reloadData()
    }
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        selectedItems.removeAll { $0 == items[indexPath.section][indexPath.row] }
        tableView.reloadData()
    }
}

Here I want to select row in a section, assume that Pizza section contains Margarita row and deep dish pizza as well contains same value. Here we need to select both rows which are different sections. It has to match with other section has same row or not whenever user tap on rows if match, all row has to select.

karthikeyan
  • 3,821
  • 3
  • 22
  • 45

1 Answers1

1

Store selected item names in an array and reload the tableview. In cellForRowAt method check if the array has the current item or not.

var selectedItems = [String]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = items[indexPath.section][indexPath.row]
    if selectedItems.contains(items[indexPath.section][indexPath.row]) {
        print("Selected Item")
    } else {
        print("Item not selected")
    }
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
      if selectedItems.contains(items[indexPath.section][indexPath.row]) {
        print("Selected Item")
        selectedItems.removeAll { $0 == items[indexPath.section][indexPath.row]
    } else {
        print("Item not selected")
        selectedItems.append(items[indexPath.section][indexPath.row])
    }

    tableView.reloadData()
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70