UICollection View永远不会加载

时间:2016-06-30 01:16:18

标签: ios xcode swift uicollectionview

我制作了一个UICollection视图。在我的ViewController中,我有以下代码:

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代码。

我希望IN COLLECTION OF ITEMS可以打印两次。

1 个答案:

答案 0 :(得分:1)

将这些代码添加到viewDidLoad

self.itemCollectionView.delegate = self;
self.itemCollectionView.dataSource = self;