I want to do an app like a to-do app with a tableview. When I hit the add button (system provided by self.navigationItem.rightBarButtonItem = addButton) a new tableview appears and a list with given items (provided by an entity of coreData).
Now I want to set by a touch in a cell a checkmark and at the end I want to press the "save" button. For taking all selected (checked items) in the first tableview.
I thought to do it in didSelectRowAtIndexPath.
But when I scroll up and down I get checkmarks even in cells that I didn t checked. then I tried the following code:
incellForRowAtIndexPath
cell.detailObejct = (dataOfEntity[indexPath.row])
if checked[indexPath.row] == false {
cell.accessoryType = .None
}
else if checked[indexPath.row] == true {
cell.accessoryType = .Checkmark
}
and in
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.accessoryType == .Checkmark
{
cell.accessoryType = .None
checked[indexPath.row] = false
}
else
{
cell.accessoryType = .Checkmark
checked[indexPath.row] = true
}
}
but when I run that code I get an error that says array index out of range. The array is declared as
var checked = [Bool]()