如何解决如何在 Swift 中添加多个集合视图单元格,其中第一个单元格将是常量,其余单元格将取决于数组计数
我正在尝试创建一个包含集合视图的视图。集合视图有多个单元格。第一个单元格将保持不变。其余的将根据阵列计数显示。我将发布我编写的代码。我的代码中的问题是 - (以及我需要您帮助的地方) 1.单元格正在显示,但它正在获取我在 numberOfItemsInSection 函数下提供的图像计数。因此,阵列中的第一张图像未显示。 2. 如果将数组计数添加到第一个不变的单元格中,我将无法获得正确的单元格计数。
输出图像:My output Image 注意:我添加了一个视图控制器并从代码中注册了单元格
代码:
[MultipleCellCollectionViewController -
import UIKit
class MultipleCellCollectionViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet weak var multipleCellCollectionView: UICollectionView!
let cellImage: \[UIImage\] = \[#imageLiteral(resourceName: "shirts"),#imageLiteral(resourceName: "hats"),#imageLiteral(resourceName: "hat03"),#imageLiteral(resourceName: "hoodie02")\]
override func viewDidLoad() {
super.viewDidLoad()
multipleCellCollectionView.delegate = self
multipleCellCollectionView.dataSource = self
multipleCellCollectionView.register(MultipleCellCollectionViewCell.self,forCellWithReuseIdentifier: "cell1")
multipleCellCollectionView.register(TwoMultipleCollectionViewCell.self,forCellWithReuseIdentifier: "cell2")
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
// if section == 0{
// return 1
// }else {
// return cellImage.count
// }
return cellImage.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1",for: indexPath) as? MultipleCellCollectionViewCell
cell!.labelLabel.text = "HI THERE"
return cell!
}else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2",for: indexPath) as? TwoMultipleCollectionViewCell
let myImage = cellImage\[indexPath.row\]
cell!.image.image = myImage
return cell!
}
}
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeforItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width
return CGSize(width: width/2 - 2,height: width/2 - 2)
}
func collectionView(_ collectionView: UICollectionView,minimumLinespacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
}
MultipleCellCollectionViewCell-
import UIKit
class MultipleCellCollectionViewCell: UICollectionViewCell {
// @IBOutlet weak var cellView: UIImageView!
public var labelLabel: UILabel = {
let labelView = UILabel()
labelView.translatesAutoresizingMaskIntoConstraints = false
//labelView.text = "Hello"
labelView.textColor = .black
labelView.font = UIFont(name: "Georgia",size: 10.0)
labelView.textAlignment = .center
return labelView
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(labelLabel)
labelLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor,constant: -10).isActive = true
labelLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor,constant: 10).isActive = true
labelLabel.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
labelLabel.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
TwoMultipleCollectionViewCell-
import UIKit
class TwoMultipleCollectionViewCell: UICollectionViewCell {
public var image: UIImageView = {
let imageV = UIImageView()
//imageV.image = UIImage(named: "shirts")
imageV.frame.size = CGSize(width: imageV.frame.size.width,height: imageV.frame.size.height)
imageV.contentMode = .scaleAspectFit
imageV.translatesAutoresizingMaskIntoConstraints = false
imageV.clipsToBounds = true
return imageV
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(image)
image.bottomAnchor.constraint(equalTo: contentView.bottomAnchor,constant: 0).isActive = true
image.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 0).isActive = true
image.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: 0).isActive = true
image.topAnchor.constraint(equalTo: contentView.topAnchor,constant: 0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}][1]
解决方法
所以你有 1 个部分,需要 return cellImage.count + 1
为部分中的项目数,然后使用 indexPath.row - 1
在 cfiaip 中查找你的单元格图像 if indexPath.item > 0: {{1} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。