swift - 在 UICollectionView 中选择单个 UILabel

标签 swift uicollectionview uilabel uicollectionviewcell

我创建了一个水平 UICollectionView,它显示 12 个月,我需要用户选择其中之一,为此我使用了 collectionCell.isUserInteractionEnabled=trueUITapGestureRecognizer 但这会返回整个页面中的月份。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let collectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "monthCollectionView", for: indexPath as IndexPath) as! AttendenceCollectionViewCell
    collectionCell.collectionViewCellLabel.text=monthArray[indexPath.item]
    collectionCell.backgroundColor = UIColor.clear
    collectionCell.isUserInteractionEnabled=true
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(customMonth(month:tap:)))
    collectionCell.collectionViewCellLabel.addGestureRecognizer(tapGesture)
    collectionCell.collectionViewCellLabel.isEnabled=true
    collectionCell.isExclusiveTouch=true

    customMonth(month:monthArray[indexPath.item],tap: tapGesture)


    return collectionCell
}


@objc func customMonth(month:String,tap:UITapGestureRecognizer){
    print("\n",month,"\n")

输出:

Aug

Sep

Oct

Nov

Dec

Apr

Mar

Feb

Jan

最佳答案

您确实应该使用 collectionView 的委托(delegate)方法 didSelectItemAt() 而不是使用手势识别器。

关于swift - 在 UICollectionView 中选择单个 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51001517/

相关文章:

swift - 我怎样才能在需要之前将 Collection View Cells deque,因为第一次 deque 是滞后的

ios - UILabel 文本自定义

ios - swift:在 ViewController 返回后显示动画

ios - 从点击手势识别器获取标签

ios - 模拟位置 bt 在第二次运行时可用

ios - 从 XIB 加载时 UICollectionViewCell 导致崩溃

ios - 我怎样才能更新我的字典 ios Swift 中的数量值

swift - 如何使 Collection View 中的一个单元格的高度与屏幕减去导航高度相同。酒吧?

iphone - 计算 UILabel 和 UIButtons 的大小来造句?

ios - 具有文本格式的多行 TableView 单元格