为什么图像视图有时不会出现在集合视图单元格中? [英] Why are image views sometimes not appearing in collection view cells?

查看:26
本文介绍了为什么图像视图有时不会出现在集合视图单元格中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新到 Swift 3.

I just updated to Swift 3.

在更新之前,我的收藏视图运行良好.我做了推荐的更改以使编译器满意,但现在我遇到了这个问题.

My collection views were working great before the update. I did the recommended changes to make the compiler happy, but now I'm having this problem.

我的自定义 UICollectionViewCells 中的图像视图根本不再出现.以编程方式生成的图像视图和原型图像视图都没有出现.

Image views that are in my custom UICollectionViewCells are simply not appearing anymore. Neither programmatically generated image views nor prototype image views are appearing.

我已经给了图像视图背景颜色来检查我的图像是否为零.背景颜色没有出现,因此可以安全地假设图像视图根本没有出现.

I've given the image views background colors to check if my images are nil. The background colors aren't appearing, so it is safe to assume the image views are not appearing at all.

细胞本身正在出现.每个图像视图下面都有一个标签,并且标签正确显示并带有正确的文本.

The cells themselves ARE appearing. Each image view has a label underneath, and the label is displaying properly with the correct text.

最令人困惑的部分是有时会出现图像视图,但似乎没有押韵或理由来说明为什么或何时出现.

The most confusing part is that sometimes the image views DO appear, but there seems to be no rhyme or reason as to why or when.

我的代码很标准,但我还是会继续分享:

My code is pretty standard, but I'll go ahead and share it anyway:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return searchClubs.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell: HomeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell

    cell.barLabel.text = searchClubs[indexPath.row].name
    cell.imageCell.image = searchClubs[indexPath.row].image

    cell.imageCell.layer.masksToBounds = true
    cell.imageCell.layer.cornerRadius = cell.imageCell.frame.height / 2

    return cell

}

func feedSearchClubs(child: AnyObject) {

    let name = child.value(forKey: "Name") as! String    

    //Image
    let base64EncodedString = child.value(forKey: "Image")!
    let imageData = NSData(base64Encoded: base64EncodedString as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
    let image = UIImage(data:imageData! as Data)

    //Populate clubs array
    let club = Club.init(name: name, image: image!)
    self.searchClubs.append(club)

    DispatchQueue.main.async {
           self.collectionView.reloadData()
    }
}

推荐答案

从 Xcode 8 开始,您必须调用 layoutIfNeeded() 来计算大小(在您的情况下,您需要知道 cell.imageCell.frame.height)和来自 auto 的位置布局规则或使用cornerRadius的固定值.

Since Xcode 8 you have to call layoutIfNeeded() to calculate size (in your case you need to know cell.imageCell.frame.height) and position from auto layout rules or use a fixed value of cornerRadius.

cell.imageCell.layoutIfNeeded()
cell.imageCell.layer.masksToBounds = true
cell.imageCell.layer.cornerRadius = cell.imageCell.frame.height / 2

cell.imageCell.layer.masksToBounds = true
cell.imageCell.layer.cornerRadius = 5

这篇关于为什么图像视图有时不会出现在集合视图单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆