セル内に別のUICollectionView(プログラムで追加された2番目のレベル)を持つUICollectionViewCell(ページングが有効、幅は&の高さです)があります。 2番目のレベルのUICollectionViewには、xib(CustomMenuViewController)を使用してUICollectionViewCellがあります。 このxibファイルには、ボタンがあります。このボタンを押すと、別のUIViewControllerに行きたいと思っています。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")
}
}
あなたは、UIViewControllerから別のものにsegueを渡すことができます。親ViewController(あなたが望むように、Closureを使用してCollectionViewCollectionViewViewControllerを理解していれば委任してください)に、ある時点でSegueを実行する必要があります。 – Larme
私はコードを教えてくれますか?私は新しいiosアプリケーションの開発私はuiviewcontrollerオブジェクトを持っていない。私はxibファイル(xibの私はそこに持っている) –
あなたの最初のコレクションビュー表示 - ビューコントローラは、ストーリーボードにありますか? – MBN