【问题标题】:How to get a image's indexpath in CollectionView in TableView?如何在 TableView 的 CollectionView 中获取图像的索引路径?
【发布时间】:2018-06-11 07:13:35
【问题描述】:

在我的程序中,TableView的每一行都有一个Collection,每个CollectionView有几个Images。我在collectionView(_:, cellForItemAt:)方法中为图片设置了TagUITapGestureRecognizer,但是Tag只能代表图片在CollectionView中的位置。如何知道被点击的图片在Table中的位置,并将TableView的行和Collection的行传递给UITapGestureRecognizer(target:, action: #selector())方法?

【问题讨论】:

  • 将 tableView 的 indepath.row 放入 collectionview 标签。那么你可以很容易地从collectionview didselect方法中获取标签。
  • 不要在 collectionView 上使用点击手势识别器。请改用 didSelectItemAtIndexPath。在collectionView中编写一个协议并确认tableView中的方法,每次用户点击collectionView中的单元格时都会触发带有collectionView实例的委托及其单元格到tableView的indexPath。现在为了找到包含 collectionView 的 tableView 单元格,使用 tableView 的 indexPathsForRows 方法应该会有所帮助。或者您可以返回将标签设置为 collectionView 的逻辑,这将在 tableView 中表示其 indexPath,而不是返回 collectionView 返回其标签
  • @SandeepBhandari 我不想点击整个单元格,我只想点击单元格内的图片。
  • @jake-to :我假设每个图像都在UICollectionView 的单个单元格中,因此您可以使用UICollectionView didSelectItem 委托方法

标签: ios swift uitableview uicollectionview indexpath


【解决方案1】:

你可以这样做:

func imageTapped(gesture: UITapGestureRecognizer) {
    let location: CGPoint = gesture.location(in: tableView)
    let ipath: IndexPath? = tableView.indexPathForRow(at: location)
    let cellindex: UITableViewCell? = tableView.cellForRow(at: ipath ?? 
 IndexPath(row: 0, section: 0))
 }

【讨论】:

    【解决方案2】:
    let pointTable :CGPoint = sender.convert(sender.bounds.origin, to: self.upComing_tblView)
    
            let indexpath = self.upComing_tblView.indexPathForRow(at: pointTable)
    

    您可以在此处找到表格视图单元格行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 2014-01-18
      • 2014-01-20
      相关资源
      最近更新 更多