我做了一个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代码。我希望在“收藏品”中打印两次。
最佳答案
将这些代码添加到viewDidLoad
self.itemCollectionView.delegate = self;
self.itemCollectionView.dataSource = self;
关于ios - UICollectionView永远不会加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38112466/