Ios 函数collectionView函数didSelectItemAt不工作
我有一个故事板: 该代码:Ios 函数collectionView函数didSelectItemAt不工作,ios,swift,swift3,swift4,Ios,Swift,Swift3,Swift4,我有一个故事板: 该代码: @IBOutlet weak var collectionView1: UICollectionView! @IBOutlet weak var collectionView2: UICollectionView! var selectedTipId: Int? var selectedleafletId: Int? @IBAction func TipDetailBtnPressed(_ sender: Any) {
@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-是左侧集合视图
collectionView2-是右侧的集合视图
TipDetailBtnPressed和TIPDetailPdbtnpressed-这是uiimage和text标签下的按钮
当我点击此按钮时-我得到结果:
@@@@@@零我零
错误消息
函数collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath)-从不显示:“我在这里”
我有以下问题:
1.为什么我没有留言“我在这里”?
2.然后我按下了:tipdetailbtnppressed和tipdetailpdfbtnppressed-为什么我不在selectedTipId和SelectedFalioID变量中获取值?我总是有这样的信息:@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
错误消息
?是:您需要在单元格类中创建按钮出口,您可以使用委托对按钮执行操作。OK,我成功了:-我现在应该做什么?:)非常简单,如果有任何问题,请按照答案告诉我
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
}