2017-02-09 10 views
1

iPadアプリで4つのCollectionViewを隣り合わせに表示する必要があります。コレクションビューの高さが画面の3/4である必要があり、そのレイアウトはこのようなものになります。カスタムコレクションビューのセルがタップで消える

___________________ 
| top content | 
|-------------------| 
| CV | CV | CV | CV | 
|____|____|____|____| 

を私は、すべてを絞り込むしようとしたこれらのコレクションビューのひとつで新しいプロジェクトを作成しました私は同じ問題にぶち当たっています。細胞の1つをタップすると、それらのすべてが消えます。再現するために:

  • 言語
  • セットアップとしてスウィフトを使用してテンプレート「シングルビューアプリケーション」を使用して新しいプロジェクトを作成するストーリーボード:ストーリーボード
    • ドラッグ新しいコレクションビューコントローラを設定ストーリーボードIDを "CollectionViewController"に設定
    • セルの場合:MyCollectionViewCellに識別子を設定し、セルにラベルをドラッグして制約を設定する
  • 以下のソースコードでファイルを作成:

CollectionViewCell.swift:(ソースコードにラベルをCtrlキーを押しながらドラッグすることで、コンセントを作成する)

import UIKit 

class CollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var label: UILabel! 

} 

CollectionViewController.swift:(有料viewDidLoadの実装にコメント)

import UIKit 

private let reuseIdentifier = "MyCollectionViewCell" 

class CollectionViewController: UICollectionViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Register cell classes 
     // this has to be removed to work with a custom cell class 
     // self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: UICollectionViewDataSource 

    override func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

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

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell 
     cell.label.text = "\(indexPath.section)-\(indexPath.row)" 
     return cell 
    } 

} 

変更ViewController.swiftの実装への注意:

import UIKit 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     let frame = view.frame 
     let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "CollectionViewController") 
     vc.view.frame = CGRect(x: 0.0, y: frame.size.height * 0.25, width: frame.size.width, height: frame.size.height * 0.75) 
     self.view.addSubview(vc.view) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

は、ここでのiOSシミュレータのキャプチャです:

https://youtu.be/VVBsTnYLGM4

私は問題を再現するために何かを忘れていない願っています。その場合、私は自分のDropboxにプロジェクトをアップロードしました。

https://dl.dropboxusercontent.com/u/607872/CollectionViewBugZip.zip

誰かが私が間違ってやっているものを私に伝えることができますか?

+0

今それを見て;) – CZ54

+0

私はちょうど何か他のものに気づい:

これはトリックをした私はコレクションビューとそのデータソース/デリゲートの間の接続を削除するにもかかわらず、それにもかかわらず、細胞は示されている。したがって、暗黙的なデータソースとデリゲートが必要ですが、どのように/理由を把握することはできません。 – Dennis

+0

https://github.com/danielgalasko/DGSelfSizingCollectionViewCells/tree/master – CZ54

答えて

5

コレクションビューをサブビューとして追加していたのは間違いでしたが、それは良い方法ではないと思われます。明らかに、サブビューだけが追加されると、そのビューコントローラから「切断されました」。 ViewController.swift

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let frame = view.frame 
    let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "CollectionViewController") 
    self.addChildViewController(vc) // <----------- !!!! 
    vc.didMove(toParentViewController: self) // <-- !!!! 
    vc.view.frame = CGRect(x: 0.0, y: frame.size.height * 0.25, width: frame.size.width, height: frame.size.height * 0.75) 
    self.view.addSubview(vc.view) 
} 
+0

これは助けてくれてありがとう – user1244109

関連する問題