选择行其他部分在UITATIONVIEW中包含相同的值



我有多个部分,每个部分都可以有多个行。

代码:显示为异常。

 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()
    }
}

在这里,我想在部分中选择行,假设披萨部分包含 Margarita 行和 deep Dish Dish Pizza 也包含相同的值。在这里,我们需要选择两个不同部分的。它必须与其他部分匹配具有相同的行,否则每当用户点击时,所有行都必须选择。

将选定的项目名称存储在数组中,然后重新加载tableview。在cellForRowAt中,方法检查数组是否具有当前项目。

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()
}

最新更新