Выберите ВСЕ строки TableView в нескольких разделах - PullRequest
1 голос
/ 24 сентября 2019

У меня есть кнопка в виде таблицы. Я хочу, чтобы при нажатии этой кнопки все строки ячеек выбирались, как это сделать?У меня есть готовый код, и я пытаюсь сделать эту функцию.Пожалуйста, помогите мне!

У меня есть массив:

var days: [String] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]


tableView.dequeueReusableCell(withIdentifier: "selectall", for: indexPath) as! SelectAllTableViewCell

        cell.selectallBtn.addTarget(self, action: #selector(checkAllSelection(_:)), for: .touchUpInside)
        return cell
    } else if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "oprearateCell", for: indexPath) as UITableViewCell
        return cell
    } else if indexPath.section == 2 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "daycell", for: indexPath) as UITableViewCell as! DaysTableViewCell
        cell.dayLabel.text = self.days[indexPath.row]
        if selectedRows.contains(indexPath)
        {
            cell.checkmark.image = UIImage(named:"checkBoxMaterial")
        }
        else
        {
            cell.checkmark.image = UIImage(named:"checkBoxOutlineBlankMaterial")

        }
        cell.checkButton.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
        cell.checkButton.tag = indexPath.row
        return cell
    }}

здесь мой код для множественного выбора

@objc func checkBoxSelection(_ sender:UIButton)
{
    let selectedIndexPath = IndexPath(row: sender.tag, section: 2)
    if self.selectedRows.contains(selectedIndexPath)
    {
        self.selectedRows.remove(at: self.selectedRows.index(of: selectedIndexPath)!)
        print(self.days[selectedIndexPath.row])
        let getDays = self.days[selectedIndexPath.row]
        operationDays.remove(object: getDays)
        print("remove",operationDays)
    }
    else
    {
        self.selectedRows.append(selectedIndexPath)
        operationDays.append(self.days[selectedIndexPath.row])
        print(operationDays)
    }
    self.tableView.reloadData()}

здесь мой код для выбора всех

@objc func checkAllSelection(_ sender:UIButton)
{
    let selectedIndexPath = IndexPath(row: sender.tag, section: 0)
    if self.selectedRows.contains(selectedIndexPath)
    {
        selectAll = false
        self.selectedRows.remove(at: self.selectedRows.index(of: selectedIndexPath)!)
    }
    else
    {
        selectAll = true
        self.selectedRows.append(selectedIndexPath)
    }
    self.tableView.reloadData()
}

и когда вы нажмете кнопку, это будет правда

но я не знаю, в какую функцию я должен поместить это?и я не знаю, как выбрать все строки

...