Swift:UICollectionView选择单元格索引问题
我正在尝试创建一个Swift:UICollectionView选择单元格索引问题,swift,uicollectionview,uicollectionviewcell,didselectrowatindexpath,Swift,Uicollectionview,Uicollectionviewcell,Didselectrowatindexpath,我正在尝试创建一个集合视图,其中有人选择一个单元格,并为每个选择将他们带到另一个保存选择内容的视图控制器。然而,当我将这行代码应用于didSelectItemAtIndexPath时,我遇到了一些困难 self.performSegueWithIdentifer("showDetail", sender: self) 然后在模拟器中运行它,单元格选择根据索引XPath工作,但每次我选择新的单元格时都会记住选择。例如,每个单元格都有一张照片和标签,如果我在indepath中选择了第一个单元格,则
集合视图
,其中有人选择一个单元格
,并为每个选择将他们带到另一个保存选择内容的视图控制器
。然而,当我将这行代码应用于didSelectItemAtIndexPath时,我遇到了一些困难
self.performSegueWithIdentifer("showDetail", sender: self)
然后在模拟器中运行它,单元格
选择根据索引XPath
工作,但每次我选择新的单元格
时都会记住选择。例如,每个单元格都有一张照片和标签,如果我在indepath
中选择了第一个单元格,则segue
会首先将我带到空白视图,然后带到我选择的单元格。如果选择另一个<代码>单元<代码>,在<代码>索引路径>代码> 3,空白视图现在是我以前选择的第一个<代码>单元格< /代码>,之后我选择了第三个<代码>单元格< /代码>。每次都是这样。如果我删除performsguewithidentifer
代码(从Xcode 6.2(在6.1.1中它是随机的)),则选择是我以前的选择,而不是我的“selectedCell”,但至少它只选择一次而不是两次才能进入视图。indepath
上出现问题。这是我的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
}
}
我被困在该做什么和应用什么解决方案上。我是否保留performsguewithidentifer
code并创建equalable
以在indepath
上实现查找(数组、选择)
?或者我可以编写一个循环(看起来容易得多),根据选择运行indepath
,并删除不再选择的单元格。但是,我不确定在循环中写入什么条件,因为我不知道“selectedCell”属性的值,因为它是可选的
for (index, value) in enumerate(cellItems) {
//something here to remove 'selectedItem' in the indexPath
}
如果我从didSelectItemAtIndexPath
中删除performsguewithidentifer
code,我可以在prepareForSegue
中执行哪些操作以获得正确索引路径上的选择
在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)
}
我已尝试将performsguewithidentifer
中的发件人更改为indepath
,但问题仍然存在
编辑我的CollectionViewController的2个完整代码
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-修订
我尝试了你的建议,但不幸的是,问题仍然存在于双视图上——在将我带到实际选定的单元格之前,它仍在传递两个视图。我还在didSelectItemAtIndexPath
中稍微修改了代码,但仍然没有解决问题
if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? PhotoViewCell {
performSegueWithIdentifier("showDetail", sender: cell)
}
然而,根据您的其他建议,在我的<代码>情节提要
中,我已经从我的<代码>集合视图
单元格
添加了一个<代码>片段
,它的标识符为“showDetail”。如果我删除segue
则无法从我的单元格中选择任何内容
虽然performsguewithidentifer
代码似乎是双视图的触发器,因为当我删除它时,单元格
仅被选择一次,但问题是单元格
选择的indexath
不正确,因为它是在空白视图上第一次选择的(这与showdeailsegue
?)有关),这会使我的indepath
不同步
编辑-解决
这停止了双重选择(删除了performsguewithidentifier
行):-
非常感谢您的帮助!!!!(注意:我更新了这个,以适应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)
}
然后在中准备(发件人:)
我使用了不久前创建的扩展findCollectionViewIndexPath()
我怀疑您的故事板中已经有一个序列,不需要func collectionView(,didSelectItemAtIndexPath:)
,但无论哪种方式,prepare序列都应该工作。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)")
}
在何处设置selectedImage和selectedLabel?可以在发件人自身上发送indexPath。PerformsgueWithIdentifer(“showDetail”,发件人:indexPath)或者,然后在prepareforsegue中获取具有此indexpath的数组元素。@user588125 SelecteImage和selected标签是在DidSelectItemAtIndePath中设置的。我尝试了这个自我。performSegueWithIdentifer(“showDetail”,发件人:indexpath)但是它不起作用。我如何将其写入prepareForSegue中的条件中?让element=yourArray[indexath.row];detailVC.selectedImage=element.selectedImage…如果没有实际的代码,就不能做更多的事情。在func collectionView(,didSelectItemAtIndexPath:)中显示代码。
@JefferyThomas我编辑了我的问题并添加了代码
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
}
}
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)
}
}
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)")
}