我以编程方式创建了 UICollectionView,设置委托(delegate)、数据源。我可以滚动 UICollectionView,单击单元格内的按钮,但无法选择我的单元格。 DidSelect 函数根本不触发!
我的代码:
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: CGRectGetWidth(self.view.frame) - 15, height: 360)
layout.scrollDirection = .Horizontal
layout.minimumLineSpacing = -12
// MARK: - Suggested Posts CollectionView
self.suggestedPostsCollectionView = UICollectionView(frame: CGRect(x: 0, y: self.webView.scrollView.contentSize.height - 360, width: CGRectGetWidth(self.view.frame), height: 360), collectionViewLayout: layout)
let nib = UINib(nibName: "SuggestedPostsCell", bundle: nil)
suggestedPostsCollectionView.registerNib(nib, forCellWithReuseIdentifier: "suggestedPostsCell")
suggestedPostsCollectionView.backgroundColor = UIColor.lightGrayColor()
suggestedPostsCollectionView.delegate = self
suggestedPostsCollectionView.dataSource = self
suggestedPostsCollectionView.userInteractionEnabled = true
self.webView.userInteractionEnabled = true
self.scrollView.userInteractionEnabled = true
self.scrollView.addSubview(suggestedPostsCollectionView)
我花了两周时间解决这个问题,但我不明白为什么会发生这种情况。谁能帮我吗?
最佳答案
尝试一下prepareForSegue
的函数:
let cellList = [YourObject]()
let indexPath = self.collectionView?.indexPathForCell(YourCell() as UICollectionViewCell)
let singleCell = cellList[indexPath!.row]
//example retrieving data from cell
singleCell.name = user_name
关于ios - 为什么我无法选择 CollectionView 中的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39181632/