加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift之自定义UICollectionViewCell

发布时间:2020-12-14 06:15:25 所属栏目:百科 来源:网络整理
导读:自定义UICollectionViewCell和自定义UITableViewCell差不多,不过自定义UICollectionViewCell更像自定义UIView,具体代码如下 import UIKitclass ClassifyCollectionViewCell: UICollectionViewCell { var imageView: UIImageView! var titleLabel: UILabel!

自定义UICollectionViewCell和自定义UITableViewCell差不多,不过自定义UICollectionViewCell更像自定义UIView,具体代码如下

import UIKit

class ClassifyCollectionViewCell: UICollectionViewCell {

    
   var imageView: UIImageView!
    
   var titleLabel: UILabel!
    
    override init(frame:CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        imageView = UIImageView()
        
        titleLabel = UILabel()
        titleLabel.font = UIFont.systemFont(ofSize: 13)
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.black
        
        
        self.addSubview(imageView)
        self.addSubview(titleLabel)
        
        self.backgroundColor = UIColor.white
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let frame:CGRect = self.bounds
        let imgx:CGFloat = 5.0
        let imgy = imgx
        
        let frameWidth:CGFloat = frame.size.width
        let imgWidth:CGFloat = frameWidth - (imgx * 2.0)
        
        
        self.imageView.frame = CGRect(x: imgx,y: imgy,width: imgWidth,height: imgWidth)
        
        self.titleLabel.frame = CGRect(x: 0,y:imgy+frameWidth,width: frameWidth,height: 20)
        
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读