【发布时间】:2022-02-10 16:23:36
【问题描述】:
我的 CollectionView 的项目不滚动,当我向下滚动时,第一个项目保持在它们的位置,没有任何变化,你可以在图像上看到我的意思:collectionview 下面是collectionView的代码:
func setUpCollectionView() {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 15, bottom: 20, right: 15)
layout.itemSize = CGSize(width: 100, height: 100)
myCollectionView = UICollectionView(frame: self.contentView.frame, collectionViewLayout: layout)
myCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
myCollectionView?.backgroundColor = UIColor.clear
myCollectionView?.allowsSelection = true
myCollectionView?.dataSource = self
myCollectionView?.delegate = self
contentView.addSubview(myCollectionView ?? UICollectionView())
myCollectionView?.register(SpendingCell.self, forCellWithReuseIdentifier: "Spending")
myCollectionView?.translatesAutoresizingMaskIntoConstraints = false
myCollectionView?.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
myCollectionView?.widthAnchor.constraint(equalTo: contentView.widthAnchor).isActive = true
myCollectionView?.heightAnchor.constraint(equalToConstant: 350).isActive = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Spending", for: indexPath) as! SpendingCell
cell.setUpSpending(cost: categories[indexPath.row])
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 23
}
和 collectionView 单元格:
class SpendingCell: UICollectionViewCell {
let stackView = UIStackView()
let SpendingButton = UIImageView()
let nameLabel = UILabel()
let sumLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(stackView)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setUpSpending(cost: Spending) {
stackView.alignment = .center
stackView.axis = .vertical
stackView.distribution = .fillProportionally
stackView.spacing = 5
stackView.addArrangedSubview(SpendingButton)
stackView.addArrangedSubview(nameLabel)
//Constraints
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
stackView.widthAnchor.constraint(equalToConstant: 100).isActive = true
stackView.heightAnchor.constraint(equalToConstant: 100).isActive = true
SpendingButton.image = cost.image
SpendingButton.contentMode = .scaleAspectFit
SpendingButton.layer.cornerRadius = 40
SpendingButton.layer.masksToBounds = true
SpendingButton.translatesAutoresizingMaskIntoConstraints = false
SpendingButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
SpendingButton.heightAnchor.constraint(equalToConstant: 80).isActive = true
nameLabel.text = cost.title
nameLabel.textAlignment = .center
nameLabel.textColor = .black
nameLabel.font = UIFont.boldSystemFont(ofSize: 10)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
nameLabel.heightAnchor.constraint(equalToConstant: 15).isActive = true
}
}
有人可以帮我改一下吗?提前致谢!
【问题讨论】:
-
你在哪里调用这个函数
setUpCollectionView()? -
CollectionView在tableView的一个单元格中,所以我在
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)function中调用 -
我认为这是您问题的根源。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)在您滚动 tableView 时被多次调用。每次调用setUpCollectionView()时,都会向内容视图添加一个新的集合视图,因此我相信会在彼此之上创建多个集合视图。 -
你是对的,谢谢!