How can I select and deselect all the cells from this UITableView with one click?
I'm having problems with this button. So, the idea is that when I click it, all the cells from the UITableView have a checkmark (select), and when I click it again all the cells don't have the checkmark (deselect).
I already achieved that the UIButton change the name after the click (from "Select all" to "Deselect all" and viceversa).
These are the lines I wrote:
let locationItems:[String] = ["China", "United States", "South Africa", "Spain", "Australia", "Brazil", "Colombia", "Nigeria", "India"]
var selectedItemsIndex:Int?
var selectIndicator = true
@IBOutlet var tableViewWithOptions: UITableView!
@IBOutlet var selectAllLabel: UIButton!
@IBAction func selectAllButton(sender: AnyObject) {
if (selectIndicator == true) {
if selectAllLabel.titleLabel?.text == "Select all" {
selectAllLabel.setTitle("Deselect all", forState: UIControlState.Normal)
}
selectIndicator = false
} else {
if selectAllLabel.titleLabel?.text == "Deselect all" {
selectAllLabel.setTitle("Select all", forState: UIControlState.Normal)
}
selectIndicator = true
}
tableViewWithOptions.reloadData()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let index = selectedItemsIndex {
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0))
cell?.accessoryType = .None
}
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
}
Pls, let me know. Thanks!