【问题标题】:UICollectionViewCell gets selected when i scrollUICollectionViewCell 在我滚动时被选中
【发布时间】:2021-12-28 20:44:47
【问题描述】:

screen shoot

我希望一次只选择一个选项。当我慢慢滚动时它工作正常。但是当我快速滚动时,会自动选择超过 2 个选项。 我在这里错了。 四个小时我想弄清楚我不知道请任何人告诉我我错在哪里

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.menuProperties.menuTitles.count
}

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

    
    let isSelected = indexPath == self.selectedItemIndex ? true : false
    cell.create(indexPath:indexPath,isSelected:isSelected,menuProperties:self.menuProperties)
    
    return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let menuCell = collectionView.cellForItem(at: self.selectedItemIndex) as? HMenuCell {
        menuCell.removeSelection()
    }
    self.selectedItemIndex = indexPath
    if let menuCell = collectionView.cellForItem(at: indexPath) as? HMenuCell {
            menuCell.createSelection()
        self.menuControllerDelegate?.didSelectMenuItem(selectedIndex: indexPath)
    }
   
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    if let menuCell = collectionView.cellForItem(at: self.selectedItemIndex) as? HMenuCell {
        menuCell.removeSelection()
    }
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    if indexPath == self.selectedItemIndex {
        return false
    }
    return true
}

这是我的 UICollectionViewCell 类

class HMenuCell:UICollectionViewCell {
static let identifier = "HMenuCell"
private var menuProperties:HMenuProperties!
private var titleLabel = UILabel()
private var bottomLayer = CALayer()

private func createLabel(title:String){
    titleLabel.text = title
    titleLabel.font = UIFont.systemFont(ofSize: menuProperties.textFontSize)
    titleLabel.backgroundColor = self.menuProperties.cellBackgroundColor
    titleLabel.textColor = self.menuProperties.deselectedTextColor
    titleLabel.sizeToFit()
    titleLabel.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(titleLabel)
    titleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true
    titleLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
}
func createSelection(){
    let myWidth = titleLabel.frame.width-2
    let bottomLayer = CALayer()
        bottomLayer.frame = CGRect(x: 0, y: titleLabel.frame.height+20, width: myWidth, height: 5)
        bottomLayer.backgroundColor = self.menuProperties.underlinerColor.cgColor
    bottomLayer.cornerRadius = 2.5
    titleLabel.layer.addSublayer(bottomLayer)
    titleLabel.textColor = self.menuProperties.selectedTextColor
    self.bottomLayer = bottomLayer
}
func removeSelection(){
    titleLabel.textColor = self.menuProperties.deselectedTextColor
    self.bottomLayer.removeFromSuperlayer()
    self.bottomLayer = CALayer()
}
func create(indexPath:IndexPath,isSelected:Bool,menuProperties:HMenuProperties){
    self.menuProperties = menuProperties
    createLabel(title: menuProperties.menuTitles[indexPath.row])
    if isSelected{createSelection()}
    else {removeSelection()}
}

}

【问题讨论】:

  • 你犯了最常见的错误。只需在 cellForItemAt 中写 else 条件,这对你来说会很好

标签: ios swift uicollectionview uikit


【解决方案1】:

正如我们所知,在集合中,我们正在使用dequeReusable 单元格的表格视图,它们对它们的行为是不言自明的。 从cellForItemAt 中删除选择相关代码并管理模型内的选择状态并在选择后重新加载单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多