Swift: UICollectionView 选择单元格 indexPath 问题

Swift: UICollectionView selecting cell indexPath issues

我正在尝试做一个 Collection View,其中某人 select 一个 cell,并且对于每个 selection,它会将他们带到另一个 View Controller 持有select离子的含量。但是,一旦我将这行代码应用到 didSelectItemAtIndexPath,我就会 运行 陷入困境;

self.performSegueWithIdentifer("showDetail", sender: self)

然后 运行 它在模拟器中 cell selection 根据 indexPath 工作,但每次我都会记住 selection select 新 cell。因此,例如每个 cell 都有一张照片和标签,如果我 select 第一个 cellindexPath 中, segue 将我首先带到空白视图,然后到我的 selected cell。如果我 select 另一个 cellindexPath 上的第 3 个空白视图现在是我之前选择的第一个 cell,然后它会进入我的 select编辑第三 cell 。它每次都这样做。如果我删除 performSegueWithIdentifer 代码(来自 Xcode 6.2(在 6.1.1 中它是随机的)),selection 是我之前的选择,而不是我的 'selectedCell',但是至少只有 select 一次而不是两次才能达到 viewindexPath 上出现问题。这是我的 prepareForSegue

的代码
override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
      if segue.identifer == "showDetail" {
          let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
             detailVC.selectedImageName = selectedImage
             detailVC.selectedLabel = selectedLabels
             }
      }

我不知道要做什么以及要应用什么解决方案。我是否保留 performSegueWithIdentifer 代码并创建一个 Equatable 以在 indexPath 上实现 find(array, selection)?或者我可以写一个循环,(这似乎更容易),它会 运行 通过 indexPath 基于 select 离子,这将删除不再是 [=100 的单元格=]编辑。但是我不确定在循环中写什么条件,因为我不知道 'selectedCell' 的 属性 的值,因为它是可选的。

for (index, value) in enumerate(cellItems) {
//something here to remove 'selectedItem' in the indexPath
}

如果我从 didSelectItemAtIndexPath 中删除 performSegueWithIdentifer 代码,我可以在我的 prepareForSegue 中做什么来获得正确索引路径上的 selection?

didSelectItemAtIndexPath

处编辑完整代码
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
     selectedImage = cellImages[indexPath.row] as String
     selectedLabels = cellLabels[indexPath.row] as String
     self.performSegueWithIdentifer("showDetail", sender: self)
   }

我已经尝试将 performSegueWithIdentifer 中的发件人更改为 indexPath,但问题仍然存在。

编辑 2 我的 CollectionViewController 的完整代码

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!

var selectedImage = String()
var selectedLabels = String()

var cellImages:[String] = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "13.jpg", "14jpg"]

var cellLabels:[String] = ["Photo 1", "Photo 2", "Photo 3", "Photo 4", "Photo 5", "Photo 6", "Photo 7", "Photo 8", "Photo 9", "Photo 10", "Photo 11", "Photo 12", "Photo 13", "Photo 14"]

func collectionView(collectionView: UICollectionView, numberOfNumberItemsInSection: Int) -> Int {
    return cellImages.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: PhotoViewCell = collectionView.dequeueReuseableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as PhotoViewCell 
    cell.labelCell.text = cellLabels[indexPath.row]
    cell.ImageCell.image = UIImage(named: cellImages[indexPath.row])
    return cell
}

 override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
     selectedImage = cellImages[indexPath.row] as String
     selectedLabels = cellLabels[indexPath.row] as String
     self.performSegueWithIdentifer("showDetail", sender: self)
   }

override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
      if segue.identifer == "showDetail" {
          let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
          detailVC.selectedImageName = selectedImage
          detailVC.selectedLabel = selectedLabels
          }
      }
}

PhotoViewCell

class PhotoViewCell: UICollectionViewCell {

@IBOutlet var labelCell: UILabel!
@IBOutlet var ImageCell: UIImage!

}

编辑 3 - 修改

我尝试了你的建议,不幸的是,问题仍然存在于双重视图上 - 在我进入实际的 selected cell 之前,它仍然通过两个视图。我也在 didSelectItemAtIndexPath 中稍微修改了代码,但它仍然没有解决问题。

if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? PhotoViewCell {

performSegueWithIdentifier("showDetail", sender: cell) 
}

然而,根据您的其他建议,在我的 StoryBoard 中,我从我的 Collection View cell 添加了一个 segue 到我的 DetailViewController,其中有标识符 "showDetail"。如果我删除 segue,则无法从我的 cells 中编辑任何内容 select。

尽管 performSegueWithIdentifer 代码似乎是双重视图的触发器,因为当我删除它时,cell 只被 selected 一次,问题是cell selection 的 indexPath 不正确,因为它首先 select 在空白视图上显示(与 showDetail segue 有关吗?) ,然后使我的 indexPath 不同步。

编辑 - 已解决

这停止了双 select 离子(删除了 performSegueWithIdentifier 行):-

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
cellLabels[indexPath.row] as String
cellImages[indexPath.row] as String
   }
}

非常感谢您的帮助!!!!

(注意:我针对 Swift 4 种及更多现代实践更新了此内容。)

我尽可能坚持UIView对象。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    guard let cell = collectionView.cellForItem(at: indexPath) else { return }

    performSegue(withIdentifier: "showDetail", sender: cell)
}

然后在prepare(for:sender:)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.identifier {
    case "showDetail":
        guard let indexPath = (sender as? UIView)?.findCollectionViewIndexPath() else { return }
        guard let detailViewController = segue.destination as? DetailViewController else { return }

        detailViewController.selectedImageName = cellImages[indexPath.row]
        detailViewController.selectedLabel = cellLabels[indexPath.row]

    default: return
    }
}

我使用了前一段时间创建的扩展程序findCollectionViewIndexPath()

extension UIView {

    func findCollectionView() -> UICollectionView? {
        if let collectionView = self as? UICollectionView {
            return collectionView
        } else {
            return superview?.findCollectionView()
        }
    }

    func findCollectionViewCell() -> UICollectionViewCell? {
        if let cell = self as? UICollectionViewCell {
            return cell
        } else {
            return superview?.findCollectionViewCell()
        }
    }

    func findCollectionViewIndexPath() -> IndexPath? {
        guard let cell = findCollectionViewCell(), let collectionView = cell.findCollectionView() else { return nil }

        return collectionView.indexPath(for: cell)
    }

}

我怀疑你已经在情节提要中有一个 segue,不需要 func collectionView(, didSelectItemAtIndexPath:),但不管怎样,prepare segue 应该可以工作。

Swift 3.0

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let iPath = self.collectionView.indexPathsForSelectedItems
    let indexPath : NSIndexPath = iPath![0] as NSIndexPath
    let rowIndex = indexPath.row
    print("row index = \(rowIndex)")
}