我在主视图中嵌入了一个UICollection视图,我正在尝试将已下载的数据提供给数组。但是,CollectionView委托(在与主VC不同的类上定义)似乎没有运行。不能有人指出我做错了吗?
let reuseIdentifier = "Cell"
class DiarioCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if defaults.boolForKey("gotPL") {
print(defaults.arrayForKey("playlistTitles")!.count) //This is not being called..
return defaults.arrayForKey("playlistTitles")!.count
}
print("No Data") //This is also not being called..
return 0
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
print(indexPath)
print(indexPath.item)
print(indexPath.row)
if defaults.boolForKey("gotPL") {
let thumbIV = cell.viewWithTag(1) as! UIImageView
let playTitle = cell.viewWithTag(2) as! UILabel
thumbIV.image = UIImage(data: defaults.arrayForKey("playlistThumbnails")![indexPath.row] as! NSData)
playTitle.text = defaults.arrayForKey("playlistTitles")![indexPath.row] as? String
return cell
}
return cell
}
}
(UICollectionView在我的主ViewController中定义为类DiarioCollectionView的实例)
答案 0 :(得分:0)
您在哪里加载数据? 集合视图的委托应该是您的主视图。 如果你所指的mainView是一个viewcontroller,那么它应该同时实现dataSource和delegate协议。
您还需要设置collectionView的delgates(UICollectionViewDelegate和UICollectionViewDataSource协议)
您可以在故事板中或通过代码设置这两个代理:
self.YOURCOLLECTIONVIEW.dataSource = self
self.YOURCOLLECTIONVIEW.delegate = self