我在ViewController中有UICollection视图,它根本没有响应didSelectItemAtIndexPath。
// super class
class ViewController: UIViewController, iCarouselDelegate, iCarouselDataSource, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
// delegate
override func viewDidLoad() {
super.viewDidLoad()
// collection view delegate and datasource
collectionView.delegate = self
collectionView.dataSource = self
// did select item
func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!) {
print(indexPath)
}
答案 0 :(得分:2)
几个猜测可能会有所帮助:
您是否意外覆盖了- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
?
确保将collectionView.userInteractionEnabled
设置为true
如果某个高优先级UIResponder
(如UIGestureRecognizer
和UIButton
添加到单元格或其子视图中,则应调用相应的方法
答案 1 :(得分:0)
我不得不用另一种方式逃避这个问题并及时运送我的项目。
我在我的单元格中创建了一个按钮,在这个方法中使用了“cellForItemAtIndexPath”并为其添加了目标:
imageButton.addTarget(self, action: #selector(ViewController.imageButtonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
并在此按钮标记中传递选定的indexPath.row,如下所示:
imageButton.tag = indexPath.row
这是我的行动:
func imageButtonAction(sender: UIButton) {
let categoryId = categories[sender.tag]["id"]
let categoryName = categories[sender.tag]["category_name"]
let mainCategory = self.storyboard?.instantiateViewControllerWithIdentifier("MainCategoryViewController") as! MainCategoryViewController
mainCategory.mainCategoryId = categoryId!
mainCategory.title = categoryName!
self.navigationController!.pushViewController(mainCategory, animated: true)
}