Swift:UICollectionView选择单元格索引路径问题

时间:2015-03-21 13:11:47

标签: swift uicollectionview uicollectionviewcell didselectrowatindexpath

我正在尝试Collection View,有人会选择cell,并且对于每个选择,它会将他们带到另一个View Controller,其中包含选择的内容。但是,当我将这行代码应用到didSelectItemAtIndexPath时,我遇到了困难;

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

然后在模拟器中运行它,cell选项正在根据indexPath运行,但每次选择新cell时都会记住选择。例如,每个cell都有一张照片和标签,如果我选择cell中的第一个indexPathsegue会先将我带到空白视图,然后再选择我选中的cell {1}}。如果我选择另一个cellindexPath上的数字3,则空白视图现在是我之前选择的第一个cell,之后我将选择第三个cell。它每次都这样做。如果我删除了performSegueWithIdentifer代码(来自Xcode 6.2(在6.1.1中它是随机的)),那么选择是我之前的选择,而不是我的选择凯尔',但至少它只选择一次而不是两次来到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以在find(array, selection)上实施indexPath?或者我可以编写一个循环(这似乎更容易),它将根据选择运行indexPath,并将删除不再选择的单元格。但是,我不确定在循环中写入什么条件,因为我不知道“selectCell”的属性值。因为它是可选的。

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

如果我从performSegueWithIdentifer中移除didSelectItemAtIndexPath代码,我可以在prepareForSegue中执行哪些操作来获取正确的indexPath选项?

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我的C​​ollectionViewController的完整代码

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 - 修订

我尝试了你的建议,不幸的是问题仍然存在于双重视图中 - 它仍然传递了两个视图,然后才将我带到实际选择的cell。我还在didSelectItemAtIndexPath稍微修改了代码,但它仍然没有解决问题。

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

performSegueWithIdentifier("showDetail", sender: cell) 
}

但是根据您的其他建议,在我的StoryBoard我已经从我的segue Collection View添加了cell到我的DetailViewController,其中包含标识符& #34; showDetail&#34 ;.如果我删除segue,则无法从我的cells中选择任何内容。

虽然似乎performSegueWithIdentifer代码是双视图的触发器,因为当我删除它时,cell只被选中一次,问题是indexPathcell选择不正确,因为它首先在空白视图上选择(与showDetail segue有关吗?​​),然后我的indexPath不同步。

编辑 - 解决了

这停止了双重选择(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
   }
}

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

2 个答案:

答案 0 :(得分:10)

(注意:我为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:),但无论如何,准备segue应该有效。

答案 1 :(得分:1)

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)")
}