简体   繁体   中英

How can I remove items from Set when uncheck a checkbox ?

Hi guys. I have a tableView , which has one checkbox and one label in all rows.

So I tagged the checkboxes with labels. The goal is to make a set with clicked checkboxes. But the problem is I've done the checking and adding in set part. But I couldn't do unselecting part. --> I mean if I click the checkbox two times basically I deselect it so I have to remove the item from set, But I could't solve it.

If you can suggest something I ll be appreciated. Thanks.

func yourCheckBoxClicked(cbx:UIButton){

    choosenSet.insert(self.tableData[cbx.tag])// this is the checkbox label which was clicked
    print(choosenSet)

}

This is how you can achieve what you want:

func yourCheckBoxClicked(cbx:UIButton) {
  let picked = tableData[cbx.tag]
  if choosenSet.contains(picked) {
    choosenSet.remove(picked) // uncheck
  } else {
    choosenSet.insert(picked) // check
  }
}

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