gpt4 book ai didi

ios - 如何链接两个相互连接的 Collection View ?

转载 作者:搜寻专家 更新时间:2023-11-01 06:21:33 24 4
gpt4 key购买 nike

我从图像选择器中获取多张图像,将其保存到两个 Collection View 中,并制作这样的 View 。

enter image description here

如您所见,有两个相互链接的集合。就像我在第二个 Collection View 中点击第二个图像时,第一个 Collection View 显示第二个 Collection View 的点击图像。那么,我如何链接两个集合相互连接的 View 。这是我从画廊或相机中选择多张图像后的详细图像选择 View 。

这可能吗?

如果有人能帮助我,我会很高兴。 :)

下一步待办事项:当我得到它时,我将在第二个 Collection View 的每个图像上添加删除按钮。并在“img_4”之后添加图像“添加按钮”,以便用户将更多图像添加到这两个 uicollection查看。

最佳答案

当您选择任何项目进入第二个 Collection View 时,您可以通过这种方式将相同的图像显示到第一个 Collection View 中:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

if collectionView == self.collectionView2{
collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
}

下面是多个 Collection View 的完整示例代码:

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var collectionView1: UICollectionView!
@IBOutlet weak var collectionView2: UICollectionView!

let collectionViewAIdentifier = "CollectionViewACell"
let collectionViewBIdentifier = "CollectionViewBCell"

var imageArray = [UIImage]()

override func viewDidLoad() {
super.viewDidLoad()

imageArray = [UIImage(named: "1.jpg")!, UIImage(named: "2.jpg")!, UIImage(named: "3.jpg")!, UIImage(named: "4.jpg")!, UIImage(named: "5.jpg")!, UIImage(named: "6.jpg")!, UIImage(named: "7.jpg")!, UIImage(named: "8.jpg")!, UIImage(named: "9.jpg")!, UIImage(named: "10.jpg")!, UIImage(named: "11.jpg")!, UIImage(named: "12.jpg")!, UIImage(named: "13.jpg")!]
print(imageArray)

collectionView1.delegate = self
collectionView2.delegate = self

collectionView1.dataSource = self
collectionView2.dataSource = self

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == self.collectionView1 {

let cellA = collectionView1.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier, forIndexPath: indexPath) as! CollectionViewCell1
cellA.imageV.image = imageArray[indexPath.row]

return cellA
}

else {
let cellB = collectionView2.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier, forIndexPath: indexPath) as! CollectionViewCell2

cellB.imageV.image = imageArray[indexPath.row]
return cellB
}
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

if collectionView == self.collectionView2{
collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}
}

}

结果:

enter image description here

项目 Sample了解更多信息。

关于ios - 如何链接两个相互连接的 Collection View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33074109/

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