我将集合视图放入表视图单元格中,并且我可以在单元格上显示但是当我想选择集合单元格(更改颜色或打印单元格编号)时,select函数不起作用,我需要多次点击单元格才能选中它。为什么单元格慢速检测所选项目?什么代码会影响要选择的单元格?
这是选择集合单元格的代码
override func awakeFromNib() {
super.awakeFromNib()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let width = UIScreen.main.bounds.width
layout.scrollDirection = .vertical
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.itemSize = CGSize(width: width/5, height: width/4)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView?.collectionViewLayout = layout
collectionView?.delaysContentTouches = false
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CategoryCollectionViewCell
cell.cateImg.image = imageName[indexPath.row]
cell.cateLabel.text! = nameArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CategoryCollectionViewCell {
cell.cateImg.image = imageName2[indexPath.row]
print("collectionViewCell selected \(indexPath)")
}
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CategoryCollectionViewCell {
cell.cateImg.image = imageName[indexPath.row]
}
}
项目拉链: https://www.dropbox.com/s/y10dgp3q61pi5n1/Finnciti.zip?dl=0
AddViewCell.swift上的问题
答案 0 :(得分:4)
我在AddViewController上删除此代码后修复了问题。
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddExpenseVC.dismissKeyboard))
view.addGestureRecognizer(tap)
@objc func dismissKeyboard() {
view.endEditing(true)
}
答案 1 :(得分:3)
答案 2 :(得分:0)
在AddViewController中,您要为视图添加手势识别器,这样可以通过点按识别器检测每个用户手势。您可以删除此手势:
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddViewController.dismissKeyboard))
对于解雇键盘,您可以实现tebleView委托方法:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dismissKeyboard()
}