Skip to content

Instantly share code, notes, and snippets.

@hpstuff
Created June 2, 2015 13:29
Show Gist options
  • Select an option

  • Save hpstuff/b52d3a69ed6fb84fbd25 to your computer and use it in GitHub Desktop.

Select an option

Save hpstuff/b52d3a69ed6fb84fbd25 to your computer and use it in GitHub Desktop.
checkbox in tablecell
class FoodTableViewCell: UITableViewCell {
@IBOutlet weak var nameLabe: UILabel!
@IBOutlet weak var checkbox: CheckBox!
}
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