2016-06-30 6 views
0

私はUICollectionビューを作成しました。私のViewControllerで私は次のコードを持っています:UICollectionビューはロードされません

import UIKit 
import AlamofireImage 

class MYViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, MYItemCellDelegate { 

    let viewModel : MYViewModel = StandPlaceViewModel() 

    @IBOutlet weak var itemCollectionView: UICollectionView! 
    @IBOutlet weak var ItemCountLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.viewModel.getItems(self.viewModel.stand) { items in 
      self.ItemCountLabel.text = String(self.viewModel.itemCount) 
      self.itemCollectionView.reloadData() 
     } 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.viewModel.tableDict.count 
    } 


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     print("IN COLLECTION OF ITEMS") 
     let cell: MYItemCell = collectionView.dequeueReusableCellWithReuseIdentifier("itemCell", forIndexPath: indexPath) as! MYItemCell 
     cell.itemTitleLabel.text = self.viewModel.tableDict[indexPath.row]![0] 
     return cell 
    } 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
     print("Cell \(indexPath.row) selected") 
    } 

... 

私の問題は、UICollectionがロードされないことです。アイテムの収集は決して印刷されません。私はコレクションと別のページを持って、それはすべて正常に動作し、私は違いを見つけることができません。

ItemCountLabel.textはviewDidLoad()で正しく設定されていますが、reloadData()はcollectionViewコードを呼び出すようなことはありません。

アイテムの収集が2回印刷されると思います。

+1

にこれらのコードを追加します) 'のviewDidLoad(の初めにself.itemCollectionView.dataSource = self'を追加してみてくださいましたか? – xinatanil

答えて

1

viewDidLoad

self.itemCollectionView.delegate = self; 
self.itemCollectionView.dataSource = self; 
+0

* .dataSourceはい、うまくいきました、ありがとうございます – Rorschach