2017-01-23 9 views
0

私はIOSの開発に新しいです。UICollectionViewをプログラム上でコンテナに追加するにはどうすればよいですか?

以下は、私が を実装したいものです - これは、コードでのUITableView

を追加し、第二の容器で - UICollectionView を追加し、第一の容器で - メインビュー に2個のコンテナを持っています私がこれまで行ってきたこと。

メインビュー:

class MainViewController:UIViewController { 

let itemView:UIView = { 
    let itemContainerView = UIView() 
    itemContainerView.backgroundColor = UIColor.lightGray 
    let collectionView = CollectionViewController() 
    itemContainerView.addSubview(collectionView.view) 
    itemContainerView.translatesAutoresizingMaskIntoConstraints = false 
    return itemContainerView 
}() 

let tableView:UIView = { 
    let tableContainerView = UIView() 
    tableContainerView.backgroundColor = UIColor.gray 
    tableContainerView.translatesAutoresizingMaskIntoConstraints = false 
    return tableContainerView 
}() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    view.backgroundColor = UIColor.white 
    view.addSubview(itemView) 
    view.addSubview(tableView) 
    setupItemView() 
    setupTableView() 
} 

func setupItemView(){ 
    itemView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true 
    itemView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true 
    itemView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 2/3).isActive = true 
    itemView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true 
} 

func setupTableView() { 
    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true 
    tableView.leftAnchor.constraint(equalTo: itemView.rightAnchor).isActive = true 
    tableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1/3).isActive = true 
    tableView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true 
} 
} 

CollectionViewController:

class CollectionViewController:UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

let cellId = "CellId" 
var colView: UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let layout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
    layout.itemSize = CGSize(width: 111, height: 111) 

    colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    colView.delegate = self 
    colView.dataSource = self 
    colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId) 
    colView.backgroundColor = UIColor.white 
    colView.translatesAutoresizingMaskIntoConstraints = false 
    self.view.addSubview(colView) 

} 

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 
    cell.backgroundColor = UIColor.brown 
    return cell 
} 

} 

私は私のメイン画面は、2個のコンテナを持って見ることができましたが、10個の収集細胞を見ることができませんでした。

誰かが正しい方向に助言できますか?前もって感謝します。私は最初のビューとしてMainViewControllerを設定すると

UPDATED

、それが正常に動作しています。 しかし、私のプロジェクトでは、MainViewControllerは最初のビューではありません。 フローは次のとおりです。 1.ユーザログイン 2.ボタン付きダッシュボードが表示されます 3.ボタンをクリックすると、ユーザがMainViewControllerにナビゲートされます。ここで

は私のダッシュボードクラスでは、ビューがロードされた後、いくつかの点でreloadDataを呼び出すようにしてください

class DashboardController: UIViewController{ 

let orderBtn:UIButton = { 
    let button = UIButton(type: .system) 
    button.backgroundColor = UIColor.init(red: 173/255, green: 184/255, blue: 255/255, alpha: 1) 
    button.layer.cornerRadius = 5 
    button.setTitle("Select Item" , for: .normal) 
    button.setTitleColor(UIColor.white, for: .normal) 
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16) 
    button.translatesAutoresizingMaskIntoConstraints = false 

    button.addTarget(self, action: #selector(handleOrderNavigation), for: .touchUpInside) 
    return button 
}() 


override func viewDidLoad() { 
    super.viewDidLoad() 
    view.backgroundColor = UIColor.white 
    view.addSubview(orderBtn) 
    setupOrderBtn() 

} 

func handleOrderNavigation() { 
    let mainViewController= MainViewController() 
    let mainViewNavController= UINavigationController(rootViewController: mainViewController) 
    self.present(mainViewNavController, animated: false, completion: nil) 
} 
} 
+0

collectionViewが 'viewDidLoad'に正しいフレームを持っているのではないかと疑います。 autolayoutを使用するか、後でフレームを設定してください。また、CollectionViewControllerを 'itemView'に追加するときにフレームを設定しません。 –

+0

@JamesP autolayoutを使ってビューをスーパービュー(コンテナ)に合わせるにはどうすればよいですか? –

答えて

0

コードは正しく機能しています。ここで私はあなたのcollectionViewコード作業から得た出力です。あなたのUIの

enter image description here

提案

  1. チェックビュー階層と見るあなたのUIで任意のcollectionViewが存在しています。

  2. メソッドが正しく呼び出されていることを確認してください。ブレークポイントを追加してチェックしてください。

編集

  1. MainViewControllerのあなたの宣言は、ボタンアクションで間違っています。
  2. の代わりにMainViewControllerを使用してください。MainViewController
  3. ボタンのクリック方法が正しくクリックされていることを確認してください。
+0

あなたが言ったように、私がMainViewを初期ビューとして設定すると、正常に動作しています。しかし、別のビューからMainViewに移動すると、そのセルは表示されません。 –

+0

@ J.GS.Han他のVCからメインVCにデータを渡していますか?あなたの仕事の流れを教えてくれますか?あなたのUIで実際に何をしたいのですか/ –

+0

私は現時点でどんなデータも渡していません。 フロー: ユーザーがログインすると、メインダッシュボードが表示されます。ユーザーがボタンをクリックすると、ユーザーはMainViewにナビゲートされます。 –

関連する問題