创建 Outlet 时 UISwitch 未正确执行 Swift

UISwitch does not execute properly when an Outlet is created Swift

我有一个 UICollectionViewCell class,其中包含 UIImageView、UILabel 和 UISwitch 的出口。 在 UICollectionViewController class 我有一个开关动作

    class CollectionViewController:UICollectionViewController{

     var cell = GridCell()

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


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

            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
    }

这是我的 GridCell class

class GridCell: UICollectionViewCell{

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

    @IBOutlet weak var switch: UISwitch!
}

当我点击开关时,只有最后一个单元格中的开关执行 switch.on 条件。

所有其他开关即使在打开开关时也执行 else 条件。

如有任何帮助,我们将不胜感激。谢谢

您的问题是您正在检查 class 中的单元格变量是否有一个打开的开关,而不是检查 sender 是否打开。对 class 中单元格变量的最后一次赋值将最终成为 CollectionView 中的最后一个单元格。另一方面,发件人将是被点击的开关。

这是一个例子:

PhotosViewController.swift:

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
    }

}

PhotosDataSource.swift:

import UIKit

struct Photo {
    var image: UIImage
    var title: String
}

class PhotosDataSource: NSObject, UICollectionViewDataSource {

    var photos: [Photo] = []

    override init() {
        super.init()

        photos.append(
            Photo(
                image: UIImage(named: "1downarrow")!,
                title: "down arrow"
            )
        )

        photos.append(
            Photo(
                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
    }
}

PhotoCollectionViewCell.swift:

import UIKit

class PhotoCollectionViewCell: UICollectionViewCell {

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

}

Assetts.xcassets:

Main.storyboard:

这是它的样子: