swift - collectionView 单元格在 tapGestureRecognizer 函数中不起作用

标签 swift uicollectionview uitapgesturerecognizer

我想在单击/点击 collectionView 单元格时更改单元格标签文本。 我尝试了以下方法,但这不起作用。

@objc func tap(_ sender: UITapGestureRecognizer) {

        let location = sender.location(in: self.collectionView)
        let indexPath = self.collectionView.indexPathForItem(at: location)
        if let index = indexPath {

            let subL = zoneDict?.sublevel[index.row]

            if (subL?.sublevel.count)! > 0 {

                DispatchQueue.main.async {
                    self.zoneDict = subL!
                    print("self.zoneDict --\(self.zoneDict!)")
                    let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "colViewCell", for: index) as! CollectionViewCell

                    cell.zoneNameLabel.text = self.zoneDict?.name // Cannot update the text label. It show the default value
                    print("zone name-- \(self.zoneDict?.name)") // Its print the result.
                }
                self.delegate?.selectedZoneWithCellItems(items: "cell")
            }
        }
}

最佳答案

我认为当你点击collectionViewCell时,iOS系统默认调用CollectionView的函数didSelectItemAtIndexPath,因此你必须通过为您的单元格注册 UITapGestureRecognizer ,之后您必须设置 View 属性 (isUserInteractionEnabled = true)。 例如:self.yourview.isUserInteractionEnabled = true

关于swift - collectionView 单元格在 tapGestureRecognizer 函数中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240096/

相关文章:

ios - Swift 文件下载问题

ios - 如何在三个 View 之间成功创建数据的向下钻取层次结构?

swift - 快速保存键值对

iphone - Swift:将字符串转换为整数

ios - [UICollectionView setCollectionViewLayout :] 上的 EXC_BAD_ACCESS

arrays - UITableView 中的 UICollectionView - 我想在 CollectionVewCell 中显示数据

ios - 多级UIView(xib类型)不触发UITapGestureRecognizer

ios - 如何知道 Swift 中的发件人标识符

ios - 在 ScrollView 内的多个 ImageView 上点击手势不触发事件,只有第一个 ImageView 点击手势起作用?

swift - 仅在按下按钮时进行 QR 扫描