gpt4 book ai didi

Swift:UICollectionView 选择单元格 indexPath 问题

转载 作者:IT王子 更新时间:2023-10-29 05:36:17 30 4
gpt4 key购买 nike

我正在尝试做一个 Collection View由此有人选择 cell对于每个选择,它会将它们带到另一个 View Controller包含选择的内容。但是,一旦我将这行代码应用于 didSelectItemAtIndexPath,我就遇到了困难。 ;

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

然后在模拟器中运行 cell选择是根据 indexPath 进行的但每次我选择新的cell时它都会记住这些选择.因此,例如每个 cell有照片和标签,如果我选择第一个 cellindexPath segue首先带我到空白 View ,然后到我选择的 cell .如果我选择另一个 cell , indexPath 上的第 3 位空白 View 现在是第一个 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实现find(array, selection)indexPath 上?或者我可以写一个循环,(这似乎更容易),它会贯穿 indexPath基于选择,这将删除不再被选中的单元格。但是我不确定在循环中写什么条件,因为我不知道'selectedCell'的属性值,因为它是可选的。

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 完整代码到我的 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 - 修改

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

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

performSegueWithIdentifier("showDetail", sender: cell)
}

但是按照你的其他建议,在我的StoryBoard我添加了一个 segue来 self 的 Collection View cell到我的DetailViewController ,它有标识符“showDetail”。如果我删除 segue从我的 cells 中无法选择任何内容.

虽然看起来是performSegueWithIdentifer代码是双重 View 的触发器,因为当我删除它时,cell只被选中一次,问题是 indexPathcell选择不正确,因为它首先在空白 View 上进行选择(这与 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
}
}

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

最佳答案

(注意:我针对 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)
}

}

我怀疑你已经在 Storyboard 中有一个 segue 并且不需要 func collectionView(, didSelectItemAtIndexPath:),但无论哪种方式,准备 segue 应该工作。

关于Swift:UICollectionView 选择单元格 indexPath 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183124/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com