我想在单击/点击 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/