swift UICollectionView的布局加载样式示例

2024-03-04  本文已影响0人  可乐小子

//
// ZZSendGiftDetailViewCell.swift
// Paper Plane
//
// Created by yaoqianghong on 2024/3/5.
//

import UIKit

let headerViewCellID = "ZZSendGiftDetailItemViewCell"

class ZZSendGiftDetailViewCell: UITableViewCell {

var collectionView: UICollectionView?
var dataArray = ["位图(0)","位图(1)","位图(2)","位图(3)","位图(4)","位图(5)","位图(6)","位图(7)"]
var selectIndex: Int = 0
typealias ZZTagCallBack = (_ tag: Int,_ name: String) -> Void
public   var  tagCallBack:ZZTagCallBack?

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

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

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func getCellHeight() -> CGFloat {
    let horticalCount = 4
    let horticalPadding = 28
    let width = (ScreenW - 32 - CGFloat((horticalCount + 1) * horticalPadding)) / 4.0
    let verticalCount = self.dataArray.count / horticalCount
    let verticalHeight = width + 4 + 17 + 16 + 5
    return 38 * 2 + CGFloat(verticalCount) * verticalHeight
}

func setView() {
    self.backgroundColor = .clear
    self.contentView.backgroundColor = .clear
    let whiteBgView = UIView()
    whiteBgView.backgroundColor = .white
    whiteBgView.layer.cornerRadius = 12
    whiteBgView.layer.masksToBounds = true
    self.contentView.addSubview(whiteBgView)
    whiteBgView.snp.makeConstraints { make in
        make.left.top.right.bottom.equalToSuperview()
    }
    whiteBgView.isUserInteractionEnabled = true
    
    // 滚动方向
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    
    // 列间距
    layout.minimumLineSpacing = 12
    // 行间距
    layout.minimumInteritemSpacing = 28
    // 设置item的四边边距
    layout.sectionInset = UIEdgeInsets(top: 12, left: 28, bottom: 12, right: 28)
    //        layout.sectionInset = .zero
    // 实例化
    let  collectionView : UICollectionView = UICollectionView.init(frame: CGRectMake(0, 20, ScreenW-32, ScreenH), collectionViewLayout: layout)
    collectionView.isUserInteractionEnabled = true
    // 添加到父视图
    whiteBgView.addSubview(collectionView)
    // 属性设置
    collectionView.autoresizingMask = .flexibleHeight
    //        collectionView.backgroundColor = .grayBackground
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(ZZSendGiftCollectionViewCell.self, forCellWithReuseIdentifier: "ZZSendGiftCollectionViewCell")
    collectionView.backgroundColor = .clear
    collectionView.delaysContentTouches = false
    self.collectionView = collectionView
}

func didClickIndex(index: Int,name: String) {
    if(self.tagCallBack != nil) {
        self.tagCallBack!(index,name)
    }
}

}

extension ZZSendGiftDetailViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize
{
    return CGSize(width: ScreenW, height: 0)
}

// 设置每组item格式
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return dataArray.count
}

// 注册cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cellString = "ZZSendGiftCollectionViewCell"
    let cell: ZZSendGiftCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellString, for: indexPath) as! ZZSendGiftCollectionViewCell
    cell.backgroundColor = .clear
    cell.contentView.backgroundColor = .clear
    cell.updateCellData(dataArray: self.dataArray, index: indexPath.item,selectIndex: self.selectIndex)
    cell.isUserInteractionEnabled = true
    cell.tagCallBack = {
        [weak self] tag,name in
        if let strongSelf = self {
            if(strongSelf.selectIndex == tag) {
                return
            }
            strongSelf.selectIndex = indexPath.item
            strongSelf.collectionView?.reloadData()
            strongSelf.didClickIndex(index: tag,name: name)
        }
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let horticalCount = 4
    let horticalPadding = 28
    let width = (ScreenW - 32 - CGFloat((horticalCount + 1) * horticalPadding)) / 4.0
    let verticalHeight = width + 4 + 17 + 16 + 5
    return CGSize(width: width, height: verticalHeight)
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

}

上一篇下一篇

猜你喜欢

热点阅读