如何在UICollectionView中获取ID记录

时间:2018-06-04 08:14:37

标签: ios swift collectionview

我有这段代码:

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

    @IBAction func TipDetailBtnPressed(_ sender: Any) {
       showSubViewInContainerView(view: "TipDetailsView", parm: 0)
    }
    @IBAction func TipDetailPDFBtnPressed(_ sender: Any) {
        showSubViewInContainerView(view: "TipDetailsPDFView", parm: 0)
    }

    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)
    }
}

// MARK: - UI
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 showSubViewInContainerView(view: String, parms: Int){
        let controller = storyboard!.instantiateViewController(withIdentifier: view)

        systemContainerView.subviews.forEach { $0.removeFromSuperview() }

        if view == "ConceptGalleryView" {
            let con =  controller as! ConceptGalleryViewController
            con.idGallery = parms
        }


        systemContainerView.addSubview(controller.view)
        addChildViewController(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false

        systemContainerView.addSubview(controller.view)

        NSLayoutConstraint.activate([
            controller.view.leadingAnchor.constraint(equalTo: systemContainerView.leadingAnchor),
            controller.view.trailingAnchor.constraint(equalTo: systemContainerView.trailingAnchor),
            controller.view.topAnchor.constraint(equalTo: systemContainerView.topAnchor),
            controller.view.bottomAnchor.constraint(equalTo: systemContainerView.bottomAnchor)
            ])

        controller.didMove(toParentViewController: self)

        self.actualVisibleView = view
    }

}

showSubViewInContainerView函数显示containerView中的视图。

我的故事板:https://i.stack.imgur.com/a39qB.png

在TipDetailBtnPressed和TipDetailPDFBtnPressed中我有功能:

showSubViewInContainerView(view: "TipDetailsView", parm: 0)

在parm中 - 我需要从collectionView项中插入单击的记录(项)的ID。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

在didselect的帮助下,我们能够找到所选择的数据并将两个变量保存在班级

  final JPanel jPanel = new JPanel( new MigLayout( "insets 0 0 0 0, gap 0 -1, wrap 4, fill",
      "0 [ right, fill ] -1 [ right, fill ] 10 [ right, fill ] -1 [ right, fill ] 0 " ) ) ;

答案 1 :(得分:0)

var lastTipId: Int?
var lastLeafId: Int?

//

 func collectionView(_ collectionView: UICollectionView, 
         didSelectItemAt indexPath: IndexPath) {

    if collectionView == collectionView1 {
       lastTipId = tipObjectArray[indexPath.row].id
    }
    else {
       lastLeafId = leafletsObjectArray[indexPath.row].id
    }
 }

// 在将var设置为函数参数

之前
if selectedTipId == nil {

   // Alert the user to select a tip

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

在其他按钮中单击