2017-08-01 125 views
0

我試圖在UICollectionView中顯示圖像。下面的代碼負責顯示單元格。CollectionView單元格不顯示

super.viewWillAppear(animated) 
    memes = appDelegate.memes 
    collectionView?.reloadData() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    let space:CGFloat = 3.0 
    let dimension = (view.frame.size.width - (2 * space))/3.0 

    flowLayout.minimumInteritemSpacing = space 
    flowLayout.minimumLineSpacing = space 
    flowLayout.itemSize = CGSize(width: dimension, height: dimension) 
    flowLayout.sectionInset = UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0) 
} 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: indexPath) as! MemeCollectionViewCell 
    let meme = memes[(indexPath as NSIndexPath).row] 

    cell.memeImageView?.image = meme.meme 
    // Configure the cell 

    return cell 
} 

我看到的是一個空白視圖控制器。當我點擊屏幕時,細節VC彈出(如它應該)。看起來好像視圖的信息在那裏,但只是沒有在屏幕上呈現。爲了確保圖像顯示,我需要做些什麼改變?

+0

是你的視圖控制器的數據源?你的cellForItem會被調用嗎? –

+0

如果你的memes.count> 0使用collectionView?.reloadData() –

+0

@JoshHamet在這種情況下,數據源是應用程序委託中的數組,在調用時,項目的單元格被調用 –

回答

0

事實證明,我沒有在storyboard中的集合視圖控制器中設置模塊名稱。

0

您的collectionview代表未被調用。 你必須寫

collectionview.delegate = self collectionview.dataSource = self

如果代表被調用時,請檢查是否有meme.meme和形象。

+0

只是試圖在這兩行代碼中添加......沒有任何變化 –

+0

因爲您的單元格是零添加 如果單元格== nil collectionView.registerNib(UINib(nibName:「MemeCollectionViewCell」, bundle:nil),forCellReuseIdentifier:identifier) cell = collectionView.dequeueReusableCellWithIdentifier(identifier)as?MemeCollectionViewCell } – anamika41

0

1)使用默認的方式提供的FlowLayout到的CollectionView

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     return CGSize(width: width/3, height: 100) 
    } 

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{ 

     return UIEdgeInsetsMake(0, 0, 0, 0) 
    } 

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{ 

     return 0 
    } 

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{ 

     return 0 
    } 

2)確保你已經在代碼初始化你重裝之前你collectionViw

collectionview.delegate = self 
collectionview.dataSource = self 

3)添加斷點numberOfItemsInSection後重新編碼和檢查你的meme數組真的有價值,你重新加載CollectionView,如果是的話,當然它會在這之後去CellforRowAt並檢查你的memeImageView是否獲得價值或不。使用斷點打印圖像視圖只是爲了嘗試

+0

我打印了對單元格圖像的描述,它是零。這很奇怪,因爲我用來顯示相同​​數據的表視圖正在顯示數據。我只是檢查收集視圖和代碼的自定義單元格中的imagview之間的連接.... ib出口似乎工作正常 –

+0

數據不會進入單元這就是爲什麼它顯示黑屏作爲查看有什麼都沒有顯示你必須在兩個步驟中檢查你的meme數組在Appdelegate中,只需檢查值是否真的在上傳數組中進行檢查;其次,如果是,則在你打算在任何地方使用你的呼籲數組打印它的描述,如count。 –

+0

我只是檢查單元格圖像被設置爲的數據。它不是零。單元格本身不是零,只是cell.imageView?.image屬性爲零。任何猜測爲什麼這是? –