2017-02-12 133 views
0

我用UICollectionViewCell与父母UICollectionViewCell内按钮:Segue公司从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

为保存集合视图的控制器创建一个协议,this协议将定义“细胞委托”,然后就可以在控制器上呼吁行动,即:

protocol DayCellDelegate 
{ 
    func buttonTapped(...) 
} 

创建在DayCellCollectionViewCell或其母公司的属性,你可以将其命名委托

var delegate : DayCellDelegate? 

而且N的cellForItem方法,当你能细胞,委托设置为控制器和执行它:

cell.delegate = self 

最后在小区的自来水行动,请拨打:

self.delegate?.buttonTapped(...) 

希望帮助,如果你需要更具体的指导让我知道。

+0

请记住为了避免任何可能的周期,使委托变量'weak'。我相信要做到这一点,协议必须仅限于'class' –

+0

DayCollectionViewCell需要从DayCellDelegate继承并符合协议,这意味着还要实现buttonTapped – unkgd