Swift 3- How to get button in UICollectionViewCell work

前端 未结 6 768
夕颜
夕颜 2020-12-09 23:19

I am trying to implement an Edit button inside a cell.

Please refer to image:

What I done so far:

MainController:

class MainCo         


        
6条回答
  •  臣服心动
    2020-12-10 00:04

    A very reliable and flexible pattern is to assign a "Callback Closure" to your cell. Put your button action handler inside the cell, and have it "call back" to the view controller.

    Here is a basic example (you should be able to implement it with your custom cell with no problem):

    //
    //  CViewWithButtonCollectionViewController.swift
    //  SWTemp2
    //
    //  Created by Don Mag on 6/5/17.
    //  Copyright © 2017 DonMag. All rights reserved.
    //
    
    import UIKit
    
    private let reuseIdentifier = "ImgItemCell"
    
    class ImgItemCell: UICollectionViewCell {
    
        // this will be our "call back" action
        var btnTapAction : (()->())?
    
        override init(frame: CGRect){
            super.init(frame: frame)
            setupViews()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setupViews()
        }
    
        let editButton: UIButton = {
            let button = UIButton(type: UIButtonType.system)
            button.translatesAutoresizingMaskIntoConstraints = false
            button.backgroundColor = .white
            button.setTitle("Edit", for: .normal)
            return button
        }()
    
        func setupViews(){
    
            // add a button
            addSubview(editButton)
    
            editButton.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
            editButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
    
            // add the touchUpInside target
            editButton.addTarget(self, action: #selector(btnTapped), for: .touchUpInside)
    
        }
    
        @objc func btnTapped() {
            print("Tapped!")
    
            // use our "call back" action to tell the controller the button was tapped
            btnTapAction?()
        }
    
    }
    
    class CViewWithButtonCollectionViewController: UICollectionViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
                layout.itemSize = CGSize(width: 300, height: 100)
            }
    
        }
    
        override func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }
        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
    
        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImgItemCell
    
            cell.backgroundColor = .red
    
            // set a "Callback Closure" in the cell
            cell.btnTapAction = {
                () in
                print("Edit tapped in cell", indexPath)
                // start your edit process here...
            }
    
            return cell
        }
    
    }
    

提交回复
热议问题