Created
June 2, 2015 13:29
-
-
Save hpstuff/b52d3a69ed6fb84fbd25 to your computer and use it in GitHub Desktop.
checkbox in tablecell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FoodTableViewCell: UITableViewCell { | |
| @IBOutlet weak var nameLabe: UILabel! | |
| @IBOutlet weak var checkbox: CheckBox! | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var foods = ["Bananas", "Orange", "Apple", "Cherry"] | |
| var selectedFoods = [] | |
| //... | |
| override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
| let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? FoodTableViewCell | |
| if let reusableCell = cell { | |
| reusableCell.nameLabel.text = foods[indexPath.row] | |
| reusableCell.checkBox.isChecked = contains(selectedFoods, foods[indexPath.row]) | |
| reusableCell.checkBox.tag = indexPath.row | |
| reusableCell.checkBox.addTarget(self, action: "select:", forControlEvents: .TouchUpInside) | |
| } | |
| return cell! | |
| } | |
| func select(sender: CheckBox){ | |
| if let index = find(selectedFoods, foods[sender.tag]) { | |
| selectedFoods.removeAtIndex(index) | |
| } | |
| else { | |
| selectedFoods.append(foods[sender.tag]) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment