我有一个UICollectionViewCell类,它包含UIImageView,UILabel和UISwitch的出口。 在UICollectionViewController类中,我有一个切换操作
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 GridCell: UICollectionViewCell{
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var switch: UISwitch!
}
当我点击开关时,只有最后一个单元格中的开关执行switch.on条件。
即使打开开关,所有其他开关也会执行else条件。
任何帮助将不胜感激。谢谢
答案 0 :(得分:0)
您的问题是您正在检查班级中的单元格变量是否有开关,而不是检查sender
是否打开。对类中单元格变量的最后一次赋值最终将成为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!
}
这是它的样子: