gpt4 book ai didi

swift - Swift 创建 Outlet 时 UISwitch 无法正确执行

转载 作者:行者123 更新时间:2023-11-30 13:14:35 24 4
gpt4 key购买 nike

我有一个 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 条件。

任何帮助将不胜感激。谢谢

最佳答案

您的问题是您正在检查类中的单元格变量是否有打开的开关,而不是检查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!

}

Assetts.xcassets: enter image description here

主 Storyboard: enter image description here

它看起来像这样:

enter image description here

关于swift - Swift 创建 Outlet 时 UISwitch 无法正确执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38370608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com