【发布时间】:2018-11-14 15:26:54
【问题描述】:
我有这个故事板:
还有这段代码:
@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 - 是正确的Collection View
TipDetailBtnPressed 和 TipDetailPDFBtnPressed - 这是 uiimage 和文本标签下的按钮。
当我点击这个按钮时 - 我有结果: @@@@@@ 无我无 错误信息
函数 collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) - 从不显示:“我在这里”
我有以下问题: 1. 为什么我没有消息“我在这里”? 2. 然后我按下:TipDetailBtnPressed 和 TipDetailPDFBtnPressed - 为什么我没有在 selectedTipId 和 selectedleafletId 变量中获得值?我总是有这样的信息:@@@@@@ nil 和 nil 错误信息 ?
【问题讨论】:
-
您需要在单元格类中创建您的按钮插座,您可以使用委托对按钮执行操作
-
好的,我成功了:pastebin.com/hKaWya3G - 我现在该怎么办?:)
-
很简单,如果有任何问题,请按照答案告诉我