从UICollectionViewCell xib到UIViewController

时间:2017-12-14 10:28:24

标签: ios swift

我有UICollectionViewCell(启用分页,宽度和高度等于视图),它在单元格内有另一个UICollectionView(第二级,以编程方式添加)。第二级UICollectionView使用xib(CustomMenuViewController)具有UICollectionViewCell。 在这个xib文件中,我有一个按钮,按下这个按钮我想转到另一个UIViewController。

class Cellnew: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{

    let cellId = "mycell";

    override init(frame: CGRect) {
        super.init(frame: frame);

        setupViews();
        self.collectionView.register(UINib(nibName: "subMenuCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "mycell")
           }

    lazy var menuBar : MenuBar = {
        let mb = MenuBar()
        return mb

    }()

    func setupViews(){
        addSubview(collectionView);

        collectionView.delegate = self;
        collectionView.dataSource = self;

        collectionView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true;
        collectionView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true;
        collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true;
        collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true;
        collectionView.heightAnchor.constraint(equalToConstant: 200).isActive = true


    }

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        layout.minimumLineSpacing = 30
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical; //set scroll direction to horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout);
        cv.backgroundColor = UIColor.hexStringToUIColor(hex: "F9F9F9")
        cv.translatesAutoresizingMaskIntoConstraints = false;
        cv.contentInset = UIEdgeInsetsMake(50, 0, 150, 0)

        return cv;
    }();


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

        let cell : subMenuCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! subMenuCollectionViewCell

        cell.mImagePress.addTarget(self,action:#selector(self.toCustommenu), for: .touchUpInside)
        return cell;
        }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(20, 10, 20, 10);
                }

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



  @objc func toCustommenu(){

      //What can i do here


                }




    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.reloadData()
        print("menutapped")
        let myDict: [String: Any] = ["CellNo": indexPath.row]
        NotificationCenter.default.post(name: .refresh, object: myDict)


    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width:( self.frame.width/2)-10-10, height: self.frame.height/3);
    }
        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

2 个答案:

答案 0 :(得分:0)

我假设您有一个初始UIViewController,其中包含故事板中最外面的集合视图。我还假设这是一个应该转换到由第二个UIViewController控制的另一个视图的视图。

由于您在集合视图中以编程方式工作,因此最好的选择是从初始视图控制器到辅助视图控制器创建一个segue并按名称触发它。

要做到这一点:

  1. 在情节提要板上找到初始视图控制器和辅助视图控制器。
  2. 控制 - 从初始视图控制器顶部栏上的黄色圆圈拖动到辅助视图。
  3. 从弹出菜单的“手动搜索”下,选择正确的选项,可能是“显示”。
  4. 单击创建的segue并为其指定标识符。
  5. 在您的代码中,每当您想要执行转换时,请在UIViewController上调用performSegue(withIdentifier:sender:)(传入您为segue指定的标识符字符串)。最外面的集合视图包含在。

答案 1 :(得分:0)

1.将一个segue从parentViewController连接到destinationViewController。

2.Parent View Controller

class parentViewcontroller : ViewController , ParentCellProtocol
{

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

    {
        Create instance of cell - parentCollectionViewCell
        //Set the delegate
        parentCollectionViewCell.parentDelegate = self
    }

    //Implement the ParentCellProtocol delegate method
    func buttonClickedFromParentCell()
    {
        //Perform segue here
    }
}

3.Parent Custom cell

protocol ParentCellProtocol 
   {
      func buttonClickedFromParentCell()
   }

   class ParentCell : ChildCellProtocol
   {
      //Create an instance of protocol.
      var parentDelegate : ParentCellProtocol?

      //Implement ChildCellProtocol Method
      func buttonClickedFromChild(){

         parentDelegate.buttonClickedFromParentCell()
      }


      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
      {
         Create instance of cell - childCustomCell
         //Set the delegate
         childCustomCell. childDelegate = self
      }
   }

4.Child Custom cell

protocol ChildCellProtocol {
    func buttonClickedFromChild()
}

class ChildCell
{
    //Create an instance of protocol.
    var childDelegate : ChildCellProtocol?

    //call the delegate method on button click.
    func toCustommenu(){

        //What can i do here
        childDelegate.buttonClickedFromChild()
    }
}

希望它可以帮到你!