【问题标题】:Swift CollectionView items do not scrollSwift CollectionView 项目不滚动
【发布时间】: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() 时,都会向内容视图添加一个新的集合视图,因此我相信会在彼此之上创建多个集合视图。
  • 你是对的,谢谢!

标签: swift uicollectionview


【解决方案1】:
func makeKeyboardCollectionView() {
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 70, height: 40)
    layout.scrollDirection = .vertical
    keyboardCollectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    contentView.addSubview(keyboardCollectionView)
    
    keyboardCollectionView.snp.makeConstraints({ make in
        make.top.equalTo(vocabularyLabel.snp.bottom).inset(-5)
        make.left.equalToSuperview().inset(40)
        make.right.equalToSuperview().inset(50)
        make.height.equalTo(210)
    })
    keyboardCollectionView.backgroundColor = .clear
    keyboardCollectionView.showsVerticalScrollIndicator = false
    keyboardCollectionView.register(KeyboardCollectionViewCell.self, forCellWithReuseIdentifier: KeyboardCollectionViewCell.identifier)
    keyboardCollectionView.dataSource = self
    keyboardCollectionView.delegate = self
}

【讨论】:

  • 好吧collectionView跟键盘没关系,它只是打开了,因为collectionView下面有textField
  • 有一个删除和数据源。我们将在委托函数上添加collectionviewcell。
猜你喜欢
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多