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

时间:2019-07-28 10:43:59

标签: 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")
            }
        }
}

3 个答案:

答案 0 :(得分:0)

您可以在包含集合视图的VC中使用它

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = segmentCollectionView.dequeueReusableCell(withReuseIdentifier: SegmentCellId, for: indexPath) as! CollectionViewCell
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(printHello))
        cell.addGestureRecognizer(tapGesture)
        return cell
    }

@objc func printHello(){
        print("Hello")
    }

答案 1 :(得分:0)

我认为,当您点击collectionViewCell的{​​{1}}时,iOS系统默认调用函数didSelectItemAtIndexPath使得您必须通过为您的寄存器注册CollectionView的方式来处理默认事件选择单元格单元格,然后必须设置视图属性(UITapGestureRecognizer)。 例如:isUserInteractionEnabled = true

答案 2 :(得分:0)

您可以以这种方式使用其委托方法来在单击单元格时更改单元格标签文本:->在此,我以两个数组为例来更新标签文本: //下面使用的变量。

let nameCapitalArr1 = [“ ABC”,“ DFG”,“ EFG”,“ HIJ”,“ KLM”,“ NOP”,“ QRS”,“ TUV”,“ WXY”,“ Z”]

let nameSmallArr2 = [“ abc”,“ dfg”,“ efg”,“ hij”,“ klm”,“ nop”,“ qrs”,“ tuv”,“ wxy”,“ z”]

var changeFlag:布尔=假

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LabelTextCollectionViewCell", for: indexPath) as? LabelTextCollectionViewCell else { return UICollectionViewCell() }

    cell.nameTextLabel.text = !changeFlag ?  nameCapitalArr1[indexPath.row] : nameSmallArr2[indexPath.row]
    return cell
}

/*That method is called when tapping on the cell and reload that particular cell and also change the label text.*/

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    changeFlag = !changeFlag
    collectionView.reloadItems(at: [indexPath])
}

输出:->在具有切换效果的单元格上点击时,将大写的单元格文本反映为较小的数组值。