gpt4 book ai didi

swift - uicollectionview 水平计数以制作日历

转载 作者:行者123 更新时间:2023-11-30 11:10:25 24 4
gpt4 key购买 nike

我想制作日历,如何将collectionview的计数从垂直更改为水平计数

这就是我目前拥有的

enter image description here

这就是我正在寻找的结果

enter image description here

您可以看到第一个图像从上到下计数,但第二个图像从左到右计数。我想换成第二个。

这是代码

我有 4 个单独的文件来使用 collectionview 和自定义 collectionviewcell 创建日历

日历集合

import UIKit

class calendarCollecction: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}

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

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("my number is \(indexPath.row)")
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.frame.width, height: self.frame.height)
}


lazy var collectionViews: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsetsMake(0,0,0,0)
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.showsVerticalScrollIndicator = false
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()

override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}

func setupViews(){
collectionViews.register(calendarCollecctionCell.self, forCellWithReuseIdentifier: "cell")
collectionViews.translatesAutoresizingMaskIntoConstraints = false
backgroundColor = UIColor.clear
addSubview(collectionViews)
collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
}

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

日历集合单元格

import UIKit

class calendarCollecctionCell: UICollectionViewCell {
let label = dateCollection()
override init(frame: CGRect) {
super.init(frame: frame)
framing()
}

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

func framing(){
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
label.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
label.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
}

}

日期集合

import UIKit

class dateCollection: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
var dateNumber = 30
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dateNumber
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! dateCollectionCell
var value = indexPath.row + 1
cell.label.text = String(value)
cell.backgroundColor = .red
return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("my number is \(indexPath.row)")
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 40, height: 40)
}

lazy var collectionViews: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 5
layout.minimumInteritemSpacing = 5
layout.sectionInset = UIEdgeInsetsMake(5,5,5,5)
layout.scrollDirection = UICollectionViewScrollDirection.horizontal
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.showsVerticalScrollIndicator = false
cv.showsHorizontalScrollIndicator = false
cv.backgroundColor = UIColor.clear
cv.dataSource = self
cv.delegate = self
return cv
}()

override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}

func setupViews(){
collectionViews.register(dateCollectionCell.self, forCellWithReuseIdentifier: "cell")
collectionViews.translatesAutoresizingMaskIntoConstraints = false
backgroundColor = UIColor.clear
addSubview(collectionViews)
collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
}

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

}

日期集合单元格

import UIKit

class dateCollectionCell: UICollectionViewCell {

let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
framing()
}

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

func framing(){
label.translatesAutoresizingMaskIntoConstraints = false
label.textAlignment = .center
addSubview(label)
label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
label.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
label.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touched "+self.label.text!)
}

}

最佳答案

只是改变flowLayout的scrollDirection

layout.scrollDirection = .vertical

关于swift - uicollectionview 水平计数以制作日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52248844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com