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中唤醒时,它就工作了