简体   繁体   中英

how to toggle boolean value in swift for changing the button icon in tableview

i had done this using dictionary .how to change the bool value in onClick method while clicking each time . // delegate method

func onClick(index:Int){

   array[index]["status"] = true
    TableView.reloadData()


}

// in tableview

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
    let  dict = array[indexPath.row]

    cell.lab.text = dict["name"] as! String
    let status:Bool = dict["status"] as! Bool
    cell.index     =  indexPath.row


    if(status == true){
        cell.btn.setImage(UIImage(named: "checked"), for: .normal)
    }else{
       cell.btn.setImage(UIImage(named: "unchecked"), for: .normal)
    }

    cell.delegate = self



    return cell
}

why have you created a dictionary when you could store the status in a variable?

var status: Bool! = true

func onClick(index:Int){

   status = status == true ? false : true
   tableView.reloadData()


}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
    let  dict = array[indexPath.row]

    cell.lab.text = dict["name"] as! String
    let status:Bool = status
    cell.index     =  indexPath.row

    cell.btn.setImage(UIImage(named: status == true ? "checked" : "unchecked"), for: .normal)

    cell.delegate = self
    return cell
}

From: Matt Neuburg Book “iOS 13 Programming Fundamentals with Swift.” :

! (not) The ! unary operator reverses the truth value of the Bool to which it is applied as a prefix. If ok is true, !ok is false — and vice versa.

A common situation is that we have a Bool stored in a var variable somewhere, and we want to reverse its value — that is, make it true if it is false, and false if it is true. The ! operator solves the problem; we fetch the variable's value, reverse it with !, and assign the result back into the variable:

v.isUserInteractionEnabled = !v.isUserInteractionEnabled

That, however, is cumbersome and error-prone. Starting in Swift 4.2 , there's a simpler way — call the toggle method on the Bool variable:

v.isUserInteractionEnabled.toggle()

Just toggle it if you're using Swift 4.2 :

func onClick(index:Int){
   array[index]["status"]?.toggle()
   tableView.reloadData()
}

If you are still on Swift 3 , you could use the negation operator ! before a Bool:

func onClick(index:Int){
   array[index]["status"] = !array[index]["status"]!
   tableView.reloadData()
}

(The ! at the end force-unwraps the value since it is optional)

To avoid force-unwrapping optionals, define the toggle function as follows and use it like in Swift 4.2:

extension Bool {
    mutating func toggle() {
        self = !self
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM