Robert Brax
Robert Brax

Reputation: 7318

Switch in a cell, not passing correct bool value in tableView method

In a cell class file I have:

class adminImagesCell: UITableViewCell {

@IBOutlet weak var banSwitch: UISwitch!

}

The outlet is linked to a switch view in a cell. In table view method that controls the delete process I have:

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.dequeueReusableCellWithIdentifier("adminImagesCell") as? adminImagesCell

    println(cell!.banSwitch.on)

   tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

When I trigger the delete from this code, cell!.banSwitch.on invariably prints FALSE, even if I set the switch on. Do you know why ?

Upvotes: 0

Views: 69

Answers (1)

rdelmar
rdelmar

Reputation: 104082

The problem is that you're dequeueing a new cell, not getting a reference to the one at the indexPath passed in. You should be using cellForRowAtIndexPath,

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.cellForRowAtIndexPath(indexPath) as? adminImagesCell

    println(cell!.banSwitch.on)

   tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

Upvotes: 1

Related Questions