我正在尝试实现一个 Collection View,其中某人选择一个单元格,对于每个选择,它都会带他们到另一个 View Controller,该 Controller 持有选择的内容。然而,我遇到了困难,因为一旦我将此行代码应用于didSelectItemAtIndexPath;
请编辑
编辑-已解决
以下操作防止了双重选择(删除了performSegueWithIdentifier行):-
self.performSegueWithIdentifer("showDetail", sender: self)
然后在模拟器中运行它,cell
选择根据indexPath
工作,但每次我选择新的cell
时它都会记住之前的选择。例如,每个cell
都有照片和标签,如果我选择indexPath
中的第一个cell
,则segue
首先会将我带到空白视图,然后再转到我的所选cell
。如果我选择另一个cell
,例如indexPath
上的第三个,则空白视图现在是我之前选择的第一个cell
,然后再转到我的所选第三个cell
。它每次都这样做。如果我从Xcode 6.2中删除performSegueWithIdentifer
代码(在6.1.1中它是随机的),则选择是我以前的选择,而不是我的“selectedCell”,但至少它只选择一次而不是两次才能进入view
。在indexPath
上出了问题。这是我的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
}
}
我被卡在如何做和应用解决方案上。我应该保留performSegueWithIdentifier
代码并创建一个Equatable
来实现在indexPath
上执行find(array, selection)
,还是可以编写一个循环(似乎更容易),根据选择运行indexPath
,然后删除不再选定的单元格。但是,因为它是可选的,所以我不确定循环中要写什么条件,因为我不知道“selectedCell”的属性值。for (index, value) in enumerate(cellItems) {
//something here to remove 'selectedItem' in the indexPath
}
如果我从didSelectItemAtIndexPath
中删除performSegueWithIdentifer
代码,我在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:我的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 - 已更正
我尝试了你的建议,不幸的是,双重视图问题仍在持续 - 在实际选择cell
之前它仍然经过了两次视图。我还稍微修改了didSelectItemAtIndexPath
中的代码,但仍然没有解决问题。
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? PhotoViewCell {
performSegueWithIdentifier("showDetail", sender: cell)
}
然而,按照你的另一个建议,在我的StoryBoard中,我从我的Collection View cell添加了一个segue到我的DetailViewController,并将其标识符设置为“showDetail”。如果我删除segue,就无法从我的cells选择任何内容。尽管似乎performSegueWithIdentifier代码是双重查看的触发器,因为当我删除它时,只能选择一次cell,但问题在于cell选择的indexPath不正确,因为它首先在空视图上进行选择(这与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
}
}
非常感谢您的帮助!!!!
func collectionView(,didSelectItemAtIndexPath:)
中的代码。 - Jeffery Thomas