Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何设置UICollectionviewCell_Swift_Uicollectionviewcell - Fatal编程技术网

Swift 如何设置UICollectionviewCell

Swift 如何设置UICollectionviewCell,swift,uicollectionviewcell,Swift,Uicollectionviewcell,我已经在故事板的集合视图单元中添加了一个图像。我想将图像设置为圆形。然而,这是行不通的 视图控制器 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",

我已经在故事板的集合视图单元中添加了一个图像。我想将图像设置为圆形。然而,这是行不通的

视图控制器

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ContentCollectionViewCell
    return cell
}
ContentCollectionViewCell

class ContentCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var userProfileImageView: UIImageView!

    required init?(coder aDecoder: NSCoder) {
        // called
        super.init(coder: aDecoder)
    }


    override init(frame: CGRect) {
        // never called
        super.init(frame: frame)
        setRoundedImage()
    }

    func setRoundedImage() {
        userProfileImageView.layer.cornerRadius = userProfileImageView.frame.width * 0.5
    }

}
但是它在视图控制器中是这样工作的

为什么
init(frame:CGRect)
inContentCollectionViewCell从未被调用?如果我想在UICollectionCell类中设置预定义内容,那么最佳实践是什么

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ContentCollectionViewCell
    cell.userProfileImageView.layer.cornerRadius = cell.userProfileImageView.frame.width * 0.5
    return cell
}

您必须为所有单元格设置它。将此代码写入您的
cellForItemAt

cell.userProfileImageView.layer.cornerRadius = cell.userProfileImageView.frame.width * 0.5
cell.userProfileImageView!.clipsToBounds = true

希望它能起作用

您应该在中添加代码的舍入行

layoutSubviews()

不在init()中。

请尝试此代码,以获得完整的帮助

牢房周围的那个//四舍五入满单元

    cell.contentView.layer.cornerRadius = 2.0;
    cell.contentView.layer.borderWidth = 1.0;
    cell.contentView.layer.borderColor = UIColor.clearColor().CGColor;
    cell.contentView.layer.masksToBounds = true;

在单元格类//单元格的圆形图像视图中添加以下内容

class ExploreCategoryCollectionViewCell: UICollectionViewCell
{

@IBOutlet var coverImage: UIImageView!
@IBOutlet var lblCoverName: UILabel!
@IBOutlet var lblArtistName: UILabel!
@IBOutlet var lblTags: UILabel!
@IBOutlet var lblE: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()

    self.coverImage.layer.cornerRadius = 5.0
    self.coverImage.layer.masksToBounds = true
}

在单元类中,您可以对单元进行任何初始设置:

class FV_Cell: UICollectionViewCell
 {
     override func awakeFromNib()
     {
         super.awakeFromNib()
           // Initialization code
    }
}

非常感谢。当把所有的东西都从NIB中唤醒时,它就工作了