Swift 3.0 multiple selection with select all cell

前端 未结 4 1977
心在旅途
心在旅途 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:13

    I like @Pranil's suggestion of using a separate section for the "All" row, so I have stolen that.

    You can use an NSMutableIndexSet for tracking the selected rows. This is simpler than having to create a new struct or array of booleans or something. The only thing you do need to be aware of is if your tableview allows row reordering then the index set needs to be adjusted accordingly.

    Here is my implementation. The "all" state is determined by the number of selected rows being equal to the number of rows in the data source array.

    I have just used simple table view accessories for the checkmarks, but I am sure you can see how to adopt your image based approach in cellForRow(at:)

    import UIKit

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        @IBOutlet weak var tableview: UITableView!
    
    
        let names: [String]? = ["Luke Skywalker","Leia Organa","Advik Shah","Aarav Modi"]
    
        var selectedRows = NSMutableIndexSet()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func numberOfSections(in tableView: UITableView) -> Int {
    
            return 2
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            guard let names = self.names else {
                return 0
            }
    
            return 0 == section ? 1 : names.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath)
    
            var text: String
            var accessory = UITableViewCellAccessoryType.none
    
            if 0 == indexPath.section {
                text = "All"
                if self.selectedRows.count == self.names!.count {
                    accessory = .checkmark
                }
            } else {
                text = names![indexPath.row]
                if selectedRows.contains(indexPath.row) {
                    accessory = .checkmark
                }
            }
    
            cell.textLabel!.text = text
            cell.accessoryType = accessory
    
            return cell
        }
    
        func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
            if indexPath.section == 0 {
                if self.selectedRows.count == self.names!.count {
                    self.selectedRows = NSMutableIndexSet()
                } else {
                    self.selectedRows = NSMutableIndexSet(indexesIn: NSRange(location: 0, length: self.names!.count))
                }
                tableView.reloadData()
            } else {
                self.selectedRows.contains(indexPath.row) ? self.selectedRows.remove(indexPath.row) : self.selectedRows.add(indexPath.row)
    
                let rows = [IndexPath(row: 0, section: 0), indexPath]
    
                tableView.reloadRows(at: rows, with: .none)
            }
            return nil
        }
    }
    

提交回复
热议问题