微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CollectionViewCell CardView 样式

如何解决CollectionViewCell CardView 样式

我已经使用代码在 UITableViewCell 内创建了一个 UICollectionView,我的目的是为每个图块(为每个 CollectionViewCell)创建一个卡片视图样式,但我没有正确获得卡片视图效果。到目前为止,这就是我所做的。

我的 CollectionViewCell 类如下

  class CollectionViewCell: UICollectionViewCell {
    let padding: CGFloat = 0
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
       
        commonInit()
    }

    func commonInit(){
        backgroundColor = .white
        self.contentView.backgroundColor = .white
        contentView.addSubview(label)
        updateConstraints()
    }

    override func updateConstraints() {
        label.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            label.topAnchor.constraint(equalTo: topAnchor,constant: 0),label.leadingAnchor.constraint(equalTo: leadingAnchor,constant: 10),label.trailingAnchor.constraint(equalTo: trailingAnchor,label.bottomAnchor.constraint(equalTo: bottomAnchor,constant: 0)
            ])
        super.updateConstraints()
    }

    public let label: UILabel = {
        let v = UILabel()
        v.textColor = .darkText
        v.minimumScaleFactor = 0.5
        v.numberOfLines = 1
        return v
    }()
  }

和 MyTableViewCell 类如下

  class ReposTableViewCell: UITableViewCell {

    public lazy var collectionView: UICollectionView = { [uNowned self] in
    
        let layout = UICollectionViewFlowLayout()
        layout.minimumLinespacing = 10
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
        collectionView.register(CollectionViewCell.self,forCellWithReuseIdentifier: "CollectionCell")
        collectionView.isScrollEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.backgroundColor = .white
        return collectionView
   }()


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

  override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
    super.init(style: style,reuseIdentifier: reuseIdentifier)
    commonInit()
   
  }

  func commonInit(){
    contentView.addSubview(collectionView)
    updateConstraints()
  }


  func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate,forSection section: Int) {
    
    collectionView.delegate = dataSourceDelegate
    collectionView.dataSource = dataSourceDelegate
    collectionView.tag = section
    collectionView.reloadData()
    
  }
  override func sizeThatFits(_ size: CGSize) -> CGSize {
    let collectionCellHeight = 50
    let rows = 5 // 5: items per row
    let height = CGFloat(collectionCellHeight * rows)
    let width = contentView.frame.size.width
    return CGSize(width: width,height: height)
  }

   override func updateConstraints() {

    collectionView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
                collectionView.topAnchor.constraint(equalTo: topAnchor),collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),collectionView.bottomAnchor.constraint(equalTo: bottomAnchor),collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),])
    super.updateConstraints()
  }
}

然后我写了一个 UIView 的扩展

       func addShadow(){
                layer.cornerRadius = 5.0
                layer.borderColor  =  UIColor.clear.cgColor
                layer.borderWidth = 5.0
                layer.shadowOpacity = 0.5
                layer.shadowColor =  UIColor.lightGray.cgColor
                layer.shadowRadius = 5.0
                layer.shadowOffset = CGSize(width:5,height: 5)
                layer.masksToBounds = true
       }

我以 cell.contentView.addShadow() 的形式访问它

目前它看起来像这样,甚至在瓷砖之间没有任何垂直分隔符。 我想要的是每个瓷砖的单独卡片视图(吉姆和保罗) 我在这里错过了什么??任何帮助将不胜感激

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。