2017-08-01 154 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属性为零。任何猜测为什么这是? –

相关问题