问题描述
回购:https://github.com/Metaxa007/RunPersonalRecord
我有一个collectionView,想通过单击一个项目来执行segue。
问题是,当我单击某个项目时,didSelectItemAt没有得到调用。我尝试了不同的模拟器和真实的设备。
尽管长按contextMenuConfigurationForItemAt
可以正常工作。
class RecordsCollectionViewController: UICollectionViewController {
override func collectionView(_ collectionView: UICollectionView,contextMenuConfigurationForItemAt indexPath: IndexPath,point: CGPoint) -> uicontextmenuconfiguration? {
let configuration = uicontextmenuconfiguration(identifier: nil,previewProvider: nil){ action in
let delete = UIAction(title: "Delete",image: UIImage(systemName: "trash.fill"),identifier: nil,discoverabilityTitle: nil,attributes: .destructive,handler: {action in
self.deleteItem(index: indexPath.item)
})
return UIMenu(title: "",image: nil,children: [delete])
}
return configuration
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
return distancesArray.count
}
override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,for: indexPath) as? RecordsCollectionViewCell {
cell.setupCell(distance: distancesArray[indexPath.item])
return cell
}
return UICollectionViewCell()
}
override func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
print("not getting called")
performSegue(withIdentifier: showRecordsTVCsegue,sender: self)
}
}
该单元格如下所示:
class RecordsCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var distanceTextView: UILabel!
let formatter = NumberFormatter()
func setupCell(distance: Int32) {
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 2
if distance >= 1000 {
self.distanceTextView.text = "\(formatter.string(from: NSNumber(value: Double(distance) / 1000)) ?? "") km"
} else {
self.distanceTextView.text = "\(distance) m"
}
self.layer.cornerRadius = 25
self.clipsToBounds = true
}
}
我在SO上尝试了不同的解决方案,但没有一个对我有帮助。 例如
collectionView.allowsSelection = true
collectionView.isUserInteractionEnabled = true
而且我也不使用手势识别器。