gpt4 book ai didi

ios - 函数 collectionView 函数 didSelectItemAt 不起作用

转载 作者:行者123 更新时间:2023-11-28 05:58:18 25 4
gpt4 key购买 nike

我有这个 Storyboard: storyboard

和这段代码:

@IBOutlet weak var collectionView1: UICollectionView!
@IBOutlet weak var collectionView2: UICollectionView!

var selectedTipId: Int?
var selectedleafletId: Int?

@IBAction func TipDetailBtnPressed(_ sender: Any) {
print("@@@@@@ \(selectedTipId) i \(selectedleafletId)")

if selectedTipId == nil {
print("Error message")
return
}

showSubViewInContainerView(view: "TipDetailsView", parm: selectedTipId!)
}

@IBAction func TipDetailPDFBtnPressed(_ sender: Any) {
// TODO: Dodać id wybranego slidera

if selectedleafletId == nil {
print("Error message")
return
}

showSubViewInContainerView(view: "TipDetailsPDFView", parm: selectedleafletId!)
}

let tipObjectArray = [
TipObject(id: 1, description: "Jakość frytek nas nie zadawala", image: UIImage(named: "a1.jpg")),
TipObject(id: 2, description: "Kolor frytek jest niesatysfakcjonujący", image: UIImage(named: "a2.jpg")),
TipObject(id: 3, description: "LOT i reklamacja", image: UIImage(named: "a3.jpg")),
TipObject(id: 4, description: "Olej nie spełnia naszych oczekiwań", image: UIImage(named: "a4.jpg")),
TipObject(id: 5, description: "jakiś fajny", image: UIImage(named: "a5.jpg"))
]

let leafletsObjectArray = [
LeafletsObject(id: 1, description: "AV-AddedValueFries-Ulotka", image: UIImage(named: "d1.jpg")),
LeafletsObject(id: 2, description: "AV-AddedValueFries-Ulotka 23112", image: UIImage(named: "d2.jpg")),
LeafletsObject(id: 3, description: "Ulotka", image: UIImage(named: "d3.jpg")),
LeafletsObject(id: 4, description: "Fajna ulotka", image: UIImage(named: "d4.jpg")),
]


override func viewDidLoad() {
super.viewDidLoad()
collectionView1.dataSource = self
collectionView1.delegate = self
collectionView2.dataSource = self
collectionView2.delegate = self
}

func showSubViewInContainerView(view: String, parm: Int){
let viewController = self.parent as! MainViewControler
viewController.showSubViewInContainerView(view: view, parms: parm)
}
}

extension TipViewController: UICollectionViewDelegate, UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

if collectionView == collectionView1 {
return tipObjectArray.count
}
else {
return leafletsObjectArray.count
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == collectionView1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! TipCollectionViewCellTips
cell.titleLabel.text = tipObjectArray[indexPath.item].description
cell.imgView.image = tipObjectArray[indexPath.item].image
return cell
}
else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! TipCollectionViewCellLeaflets
cell.titleLabel2.text = leafletsObjectArray[indexPath.item].description
cell.imgView2.image = leafletsObjectArray[indexPath.item].image
return cell
}
}



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("I am here")
if collectionView == collectionView1 {
selectedTipId = tipObjectArray[indexPath.item].id
print(selectedTipId)
}
else {
selectedleafletId = leafletsObjectArray[indexPath.item].id
print(selectedleafletId)
}
}

collectionView1 - 左边是Collection ViewcollectionView2 - 是正确的 Collection View

TipDetailBtnPressed 和 TipDetailPDFBtnPressed - 这是 uiimage 和文本标签下的按钮。

当我点击这个按钮时 - 我得到了结果:@@@@@@ 零我零错误信息

函数 collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) - 从不显示:“我在这里”

我有以下问题:1. 为什么我没有消息“我在这里”?2. 然后我按下:TipDetailBtnPressed 和 TipDetailPDFBtnPressed - 为什么我不能在 selectedTipId 和 selectedleafletId 变量中获取值?我总是收到消息:@@@@@@ nil 和 nil错误信息?

最佳答案

protocol YourVehicleViewCellDelegate: NSObjectProtocol {
func didPressEditButton()

}

// Make delegate in your cell class

weak var delegate:YourVehicleViewCellDelegate? = nil



@IBAction func button1 Tapped(_ sender: Any) {
print("write button pressed")

delegate?.didPressEditButton()

}



// call that delegate in your controller class
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == collectionView1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! TipCollectionViewCellTips
cell.delegate = self // here call your delegate
cell.titleLabel.text = tipObjectArray[indexPath.item].description
cell.imgView.image = tipObjectArray[indexPath.item].image
return cell
}

关于ios - 函数 collectionView 函数 didSelectItemAt 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50695060/

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