【发布时间】:2021-12-28 20:44:47
【问题描述】:
我希望一次只选择一个选项。当我慢慢滚动时它工作正常。但是当我快速滚动时,会自动选择超过 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