ios - 为什么我无法选择 CollectionView 中的单元格?

标签 ios swift uicollectionview uicollectionviewcell uicollectionviewdelegate

我以编程方式创建了 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/

相关文章:

ios - 您必须将 UISearchBar 放在 UITableView 中吗?

swift - 数组与偏移量对自身执行异或

ios - 动态 UIViewCell 垂直单元格滚动与水平 UICollectionViewFlowLayout

ios - 队列优先级似乎不对

ios - ReactiveCocoa 如何监听数据模型属性的变化

ios - swift 2 火力地堡 TableView

ios 为 UIcollectionview Controller 获取图像

ios - 为什么 NSIndexPath indexPathForItem 返回无效路径?

ios - 在 Spotify iOS SDK 中收听轨道结束事件

ios - Mac 应用程序的 Xcode 11 Firebase 身份验证出现错误访问钥匙串(keychain)时发生错误