【发布时间】:2015-05-24 20:12:20
【问题描述】:
我正在尝试执行Collection View,其中有人选择cell,并且对于每个选择,它会将他们带到另一个包含选择内容的View Controller。但是,一旦将这行代码应用于didSelectItemAtIndexPath,我就会遇到困难;
self.performSegueWithIdentifer("showDetail", sender: self)
然后在模拟器中运行它,cell 选择根据indexPath 工作,但每次我选择新的cell 时它都会记住选择。例如,每个cell 都有一张照片和标签,如果我在indexPath 中选择第一个cell,segue 会先让我进入空白视图,然后再转到我选择的cell。如果我选择另一个cell,则indexPath 上的第3 个空白视图现在是我之前选择的第一个cell,然后是我选择的第三个cell。它每次都这样做。如果我删除 performSegueWithIdentifer 代码(来自 Xcode 6.2(在 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
}
}
我不知道该做什么和应用什么解决方案。我是否保留performSegueWithIdentifer 代码并创建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
}
}
}
照片视图单元
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,其标识符为“showDetail”。如果我删除segue,我的cells 将无法选择任何内容。
虽然performSegueWithIdentifer 代码似乎是双重视图的触发器,因为当我删除它时,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
}
}
非常感谢您的帮助!!!!
【问题讨论】:
-
你在哪里设置 selectedImage 和 selectedLabel ?您可以在发送者 self.performSegueWithIdentifer("showDetail", sender: indexPath) 上发送 indexPath,或者然后在 prepareforsegue 中使用此 indexpath 获取数组元素。
-
@user588125 selectedImage 和 selected Label 在 didSelectItemAtIndexPath 中设置。我试过这个 self.performSegueWithIdentifer("showDetail", sender: indexPath) 但它没有用。如何将其写入 prepareForSegue 中的条件?
-
让 element = yourArray[indexPath.row]; detailVC.selectedImage = element.selectedImage...没有实际代码就做不了什么。
-
在
func collectionView(,didSelectItemAtIndexPath:)中显示代码。 -
@JefferyThomas 我已经编辑了我的问题并添加了代码
标签: swift uicollectionview uicollectionviewcell didselectrowatindexpath