Doe*_*Doe 4 ios uicollectionview uicollectionviewcell swift
我已经以UICollectionView编程方式创建了我的didSelectItemAtIndexPath方法,在这种情况下,我的方法根本不会调用。
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: CGRectGetWidth(self.view.frame), height: 360), collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.userInteractionEnabled = true
Run Code Online (Sandbox Code Playgroud)
那么,有什么问题呢?为什么当我点击单元格时我没有得到回应?
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Selected Cell: \(indexPath.row)")
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我刚刚遇到这个案例。
您是否在集合视图单元格中放置了按钮或交互式视图?您的触摸由它处理,然后 didSelect 不会被调用。
通过界面构建器关闭用户交互启用属性,或以编程方式设置 false。
小智 6
我知道这可能要晚了,但是出于功能目的,CollectionView[didSelectItem]您可能也会沉迷于对触摸的响应,addGestureRecognizer因此请检查您是否对自己的手势识别器进行了设置,CollectionView以便collectionView?.addGestureRecognizer(tapGestRecognizer)
在项目导航器中选择 ViewController.swift。更新类声明:
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
//After class declaration create a new variable:
var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Selected Cell: \(indexPath.row)")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4878 次 |
| 最近记录: |