2016-01-21 22 views
0

我在我的主視圖中嵌入了一個UICollection View,並且我試圖給它提供已經下載並放入數組的數據。然而,CollectionView委託,它是在主VC中的一個單獨的類上定義的,似乎沒有運行。有人不能指出我做錯了什麼嗎?嵌入式UICollectionView無法從主UIViewController訪問

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 defined as an instance of class DiarioCollectionView, inside my main ViewController

(UICollectionView定義爲類DiarioCollectionView的實例,我的主視圖控制器內)

回答

0

你在哪裏加載數據? 集合視圖的委託應該是您的主視圖。 如果您所指的mainView是一個viewcontroller,那麼它應該實現dataSource和delegate協議。

另外你需要設置你的CollectionView的delgates(包括UICollectionViewDelegate和UICollectionViewDataSource協議)

您可以設置這兩個代表在故事板或代碼:

self.YOURCOLLECTIONVIEW.dataSource = self 
self.YOURCOLLECTIONVIEW.delegate = self 
+0

BabyShung是正確的,你的看法控制器應成爲委託人,而不是收集視圖本身。 – Darko

+0

對!謝謝..! –