collectionView 单元格在 tapGestureRecognizer 函数中不起作用
collectionView cell not working in tapGestureRecognizer function
我想在 click/tap 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")
}
}
}
您可以在包含集合视图
的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")
}
我认为当你点击collectionViewCell
然后iOS 系统默认调用CollectionView
的函数didSelectItemAtIndexPath
所以你必须通过注册UITapGestureRecognizer
为您的单元格,然后您必须设置 属性 视图 (isUserInteractionEnabled = true
)。
例如:self.yourview.isUserInteractionEnabled = true
您可以通过这种方式使用它的委托方法来在单击单元格时更改单元格标签文本: -> 这里我以使用两个数组来更新标签文本为例:
//下面使用的变量。
let nameCapitalArr1 = ["ABC", "DFG", "EFG", "HIJ", "KLM", "NOP", "QRS" , "TUV", "WXY", "Z"]
让 nameSmallArr2 = ["abc", "dfg", "efg", "hij", "klm", "nop", "qrs" , "tuv", "wxy", "z"]
var changeFlag: Bool = false
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])
}
输出:-> 在具有切换效果的单元格上点击时,它反映了大写到小数组值的单元格文本。
我想在 click/tap 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")
}
}
}
您可以在包含集合视图
的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")
}
我认为当你点击collectionViewCell
然后iOS 系统默认调用CollectionView
的函数didSelectItemAtIndexPath
所以你必须通过注册UITapGestureRecognizer
为您的单元格,然后您必须设置 属性 视图 (isUserInteractionEnabled = true
)。
例如:self.yourview.isUserInteractionEnabled = true
您可以通过这种方式使用它的委托方法来在单击单元格时更改单元格标签文本: -> 这里我以使用两个数组来更新标签文本为例: //下面使用的变量。
let nameCapitalArr1 = ["ABC", "DFG", "EFG", "HIJ", "KLM", "NOP", "QRS" , "TUV", "WXY", "Z"]
让 nameSmallArr2 = ["abc", "dfg", "efg", "hij", "klm", "nop", "qrs" , "tuv", "wxy", "z"]
var changeFlag: Bool = false
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])
}
输出:-> 在具有切换效果的单元格上点击时,它反映了大写到小数组值的单元格文本。