我刚更新到Swift 3。
在更新之前,我的收藏视图工作得很好。我做了推荐的修改以使编译器满意,但是现在我遇到了这个问题。
自定义uiCollectionViewCells中的图像视图将不再显示。既不显示以编程方式生成的图像视图,也不显示原型图像视图。
我给了图像视图背景色,以检查我的图像是否为零。背景色不会出现,因此可以安全地假设图像视图根本不会出现。
细胞本身正在出现。每个图像视图下面都有一个标签,标签以正确的文本正确显示。
最令人困惑的是,有时图像视图确实出现了,但似乎没有押韵或原因,为什么或何时。
我的代码相当标准,但我还是会继续分享它:

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)和位置,或者使用固定的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

10-06 05:16
查看更多