从父UICollectionViewCell内的UICollectionViewCell按钮中查询

时间:2017-02-12 19:17:08

标签: ios swift segue uicollectionviewcell

我使用UICollectionViewCell与父UICollectionViewCell中的按钮:

protocol DayCellDelegate
{
    func buttonTapped()
}

class SelectTimeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var collectionViewSlider: UICollectionView!    

    override func viewDidLoad() {
        super.viewDidLoad()            
        collectionViewSlider?.register(DayCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionViewSlider.dataSource = self
        collectionViewSlider.delegate = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! DayCollectionViewCell
        cell.dayTitle.text = String(indexPath.row)
        return cell
    }
}

class DayCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, DayCellDelegate {

    func buttonTapped() {
        print("button clicked")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupDayTimeline(width: frame.width, height: frame.height)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupDayTimeline(width: CGFloat, height: CGFloat) {

        let appsCollectionView: UICollectionView = {
            let layout = UICollectionViewFlowLayout()            
            layout.itemSize = CGSize(width: 90, height: 45)
            let collectionView = UICollectionView(frame: CGRect(x: 0, y: 30, width: width, height: height - 30), collectionViewLayout: layout)            
            return collectionView
        }() 
        addSubview(appsCollectionView)

        appsCollectionView.delegate = self
        appsCollectionView.dataSource = self

        appsCollectionView.register(UINib(nibName: "TimeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "timecell")
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timecell", for: indexPath) as! TimeCollectionViewCell
        cell.btnTime.addTarget(self, action: #selector(timeSelected), for: .touchUpInside)
        }        
        return cell
    }

    func timeSelected(){
       //here I need perform segue for SelectTimeViewController
    }
}

class TimeCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var btnTime: UIButton!    
    var delegate : DayCellDelegate?

    @IBAction func btnTimeClick(_ sender: Any) {
        self.delegate?.buttonTapped()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

如何从DayCollectionViewCell执行segue? 我无法在timeSelected()中调用self.performSegue(withIdentifier:" ...",sender:info),因为UICollectionViewCell不包含performSegue

1 个答案:

答案 0 :(得分:1)

为控制器创建一个包含集合视图的协议,该协议将定义"单元委托"然后你可以在控制器上调用动作,即:

protocol DayCellDelegate
{
    func buttonTapped(...)
}

您在DayCellCollectionViewCell或其父级中创建属性,您可以将其命名为委托

var delegate : DayCellDelegate?

然后在cellForItem方法中,当你可以使用单元格时,将委托设置为控制器并实现它:

cell.delegate = self

最后,在您的手机点击操作中,请致电:

self.delegate?.buttonTapped(...)

希望有所帮助,如果您需要更具体的指导,请告诉我。