【问题标题】:UICollectionView showing images related to the selected cellUICollectionView 显示与所选单元格相关的图像
【发布时间】:2014-11-20 13:04:06
【问题描述】:

我有一个 CollectionView,我想做的是当有人点击一个单元格时,这个单元格会带你进入另一个视图,它会显示与所选单元格相关的所有图像。 我的问题是我无法评估我的所有单元格中的哪一个是所选单元格,因此我可以在下一个视图中分配与所选单元格相关的所有图像。我使用的方法出现错误...

【问题讨论】:

  • indexPath 有一个“行”属性,所以 indexPath.row == 0 indexPath.row == 1 等等
  • 对不起,这是一个集合视图,所以我认为它是 indexPath.item
  • 我试过了,但是,当我写 indexPath.item 我得到一个错误。 Xcode 自动完成不向我显示 ITEM 属性作为选项:S
  • 啊,那么 indexPath 是可选的吗?您应该阅读它们通常由objective-c的api调用返回的选项,但要快速解决您的问题,您可以使用indexPath!.item,但我强烈建议您阅读它们。

标签: ios iphone swift uicollectionview


【解决方案1】:
let route = segue.destinationViewController as DetailCollectionViewController

if let indexPath = collectionView.indexPathsForSelectedItems()?.first as? NSIndexPath {
        switch indexPath.item {
        case 0:
                route.passedArray = detailCollection1
        case 1:
                route.passedArray = detailCollection2
        default:
                break
        }
}

/* for multiple selection

// iterate over the indexPathsForSelectedItems if it is not nil
for indexPath in collectionView.indexPathsForSelectedItems() as [NSIndexPath] {
    // do your wok
}

*/

【讨论】:

    【解决方案2】:

    indexPath.row 为您提供第一个单元格。如果您有更多部分,请使用第一个 indexPath.section 来选择您选择单元格的部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多