Arfaa Shariff Arfaa Shariff - 6 months ago 23
Swift Question

UISwitch does not execute properly when an Outlet is created Swift

I have a UICollectionViewCell class that contains outlets to UIImageView,UILabel and a UISwitch.
In the UICollectionViewController class I have an action for the switch

class CollectionViewController:UICollectionViewController{

var cell = GridCell()

@IBAction func stateChanged(sender: UISwitch) {
var indexPath:NSIndexPath
indexPath = self.appCollectionView.indexPathForItemAtPoint(self.appCollectionView.convertPoint(, fromView: sender.superview))!
print("Index path",indexPath.item)

if cell.switch.on{
cell.switch.tintColor = UIColor.blueColor()
Print("Switch turned on")

cell.switch.tintColor = UIColor.redColor()
cell.switch.layer.cornerRadius = CGFloat(ApplicationConfig.cornerRaduisUISwitch)
cell.switch.backgroundColor = UIColor.redColor()

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
cell = applianceCollectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! GridCell
cell.label.text = applianceName[indexPath.row] as String
cell.image.image = UIImage(named: "ic_notifications_18pt")

return cell

And This is my GridCell class

class GridCell: UICollectionViewCell{

@IBOutlet weak var image: UIImageView!
@IBOutlet weak var label: UILabel!

@IBOutlet weak var switch: UISwitch!

When I click on the switches only the switch in the last cell executes switch.on condition.

All other switches execute the else condition even on turning on the switch.

Any help will be appreciated. Thank you


Your problem is that you are checking whether a cell variable in your class has a switch that is on rather than checking whether the sender is on. The last assignment to the cell variable in your class will end up being the last cell in the CollectionView. On the other hand, the sender will be the switch that was clicked.

Here's an example:


import UIKit

class PhotosViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    let photosDataSource = PhotosDataSource()

    @IBAction func clickedSwitch(sender: UISwitch) {
        let cell = sender.superview!.superview as! PhotoCollectionViewCell
        let labelText = cell.label.text!
        print("\(labelText) - ", terminator: "")

        sender.on ? print("On") : print("Off")

    override func viewDidLoad() {
        collectionView.dataSource = photosDataSource



import UIKit

struct Photo {
    var image: UIImage
    var title: String

class PhotosDataSource: NSObject, UICollectionViewDataSource {

    var photos: [Photo] = []

    override init() {

                image: UIImage(named: "1downarrow")!,
                title: "down arrow"

                image: UIImage(named: "5_heart")!,
                title: "5 of hearts"


    func collectionView(collectionView: UICollectionView,
        numberOfItemsInSection section: Int)
        -> Int
        return photos.count

    func collectionView(collectionView: UICollectionView,
        cellForItemAtIndexPath indexPath: NSIndexPath)
        -> UICollectionViewCell
        let identifier = "UICollectionViewCell"

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! PhotoCollectionViewCell

        let photo = photos[indexPath.row]

        cell.imageView.image = photo.image
        cell.label.text = photo.title
        return cell


import UIKit

class PhotoCollectionViewCell: UICollectionViewCell {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var label: UILabel!
    @IBOutlet var mySwitch: UISwitch!


Assetts.xcassets: enter image description here

Main.storyboard: enter image description here

Here's what it looks like:

enter image description here