【发布时间】:2020-12-12 04:55:36
【问题描述】:
回购: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, identifier: 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
而且我也不使用手势识别器。
【问题讨论】:
-
它的单元格上有一些像 UIButton 这样的视图?你的细胞长什么样?
-
我只有一个标签,显示距离。 @IBOutlet 弱变量 distanceTextView: UILabel!
-
@ArtiomLemiasheuski 尝试删除这 2 行以进行小检查:
self.layer.cornerRadius = 25,self.clipsToBounds = true -
很遗憾没有帮助
-
我抓取了你的 GitHub 存储库...一些非常奇怪的东西,我无法弄清楚。使用
Debug View Hierarchy时,它显示您的单元格已嵌入单元格?没有意义...我在情节提要中删除并重新构建了您的RecordsCollectionViewController并且(在将didSelectItemAt添加到代码中之后),它工作正常。
标签: ios objective-c swift xcode uicollectionview