Swift 3.0 multiple selection with select all cell

前端 未结 4 1986
心在旅途
心在旅途 2020-12-10 16:58

I have added data in table view and I have manually added \"select all\" option to the list at first position, now when the user selects the first option which is \'select a

4条回答
  •  借酒劲吻你
    2020-12-10 17:21

    I think you are using only one section in the table view. I suggest you use two sections in the table view, so that first section will contain only one row (Select All) and the second section will contain other options. When you click on Select All, that is in the first row of the first section you can make all the rows in the second section as selected while reloading the table view.

    // MARK: -  struct for cell item
        struct CellItem {
        var name : String
        var isSelected:Bool! = false
        init(name: String) {
               self.name = name
             }
        }
    
    class ViewController: UITableViewController {
    
    @IBOutlet var viewTable: UITableView!
    // Declare a boolean varaible to toggle the checkbox in the first section of table view
    var isSelectAllSelected : Bool = false
    var cellData: [CellItem] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        cellData = [CellItem(name: "Luke Skywalker"),CellItem(name: "Leia Organa"),CellItem(name: "Advik Shah"),CellItem(name: "Aarav Modi")]
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
             return 1
        }
        else
        {
            return cellData.count
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    // MARK: -  Table view delegates
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = TableCell()
        cell.selectionStyle = .none
    
        if indexPath.section == 0 {
            cell.textLabel?.text = "Select All"
            if isSelectAllSelected{
                cell.accessoryType = .checkmark
            }
            else{
                cell.accessoryType = .none
            }
    
        }
        else
        {
            cell.textLabel?.text = cellData[indexPath.row].name
            if cellData[indexPath.row].isSelected{
                cell.accessoryType = .checkmark
            }
            else{
                cell.accessoryType = .none
            }
    
        }
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0
        {
            cellData[indexPath.row].isSelected = !cellData[indexPath.row].isSelected
            isSelectAllSelected = cellData[indexPath.row].isSelected
            for index in cellData.indices
            {
                cellData[index].isSelected = cellData[indexPath.row].isSelected
            }
        }
        else
        {
            cellData[indexPath.row].isSelected = !cellData[indexPath.row].isSelected
            if cellData.filter({ $0.isSelected }).count == cellData.count
            {
                isSelectAllSelected = true
            }
            else
            {
                isSelectAllSelected = false
            }
    
        }
        viewTable.reloadData()
    } }
    

提交回复
热议问题