Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何链接彼此连接的两个集合视图?_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 如何链接彼此连接的两个集合视图?

Ios 如何链接彼此连接的两个集合视图?,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我从图像采集器中获取多个图像,将其保存到两个集合视图中,并创建如下视图 如你所见,有两个集合相互链接。就像我在第二个集合视图点击第二个图像时,第一个集合视图显示第二个集合视图点击的图像。所以,如何链接彼此连接的两个收藏视图。这是我从画廊或相机中拾取多幅图像后的详细图像选择视图 这可能吗 如果有人能帮我一把,我会很高兴的 TODO NEXT:当我得到这个信息时,我将在第二个收藏视图的每个图像上添加删除按钮。并在“img_4”之后添加图像“添加按钮”为了让用户在这两个uicollection视图中

我从图像采集器中获取多个图像,将其保存到两个集合视图中,并创建如下视图

如你所见,有两个集合相互链接。就像我在第二个集合视图点击第二个图像时,第一个集合视图显示第二个集合视图点击的图像。所以,如何链接彼此连接的两个收藏视图。这是我从画廊或相机中拾取多幅图像后的详细图像选择视图

这可能吗

如果有人能帮我一把,我会很高兴的


TODO NEXT:当我得到这个信息时,我将在第二个收藏视图的每个图像上添加删除按钮。并在“img_4”之后添加图像“添加按钮”为了让用户在这两个uicollection视图中添加更多图像。

当您在第二个collection视图中选择任何项目时,您可以通过以下方式在第一个collection视图中显示相同的图像:

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

    if collectionView == self.collectionView2{
        collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
    }
}
以下是多个集合视图的完整示例代码:

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)
        }
    }

}
结果:


项目以获取更多信息。

当您在第二个收藏视图中选择任何项目时,您可以通过以下方式在第一个收藏视图中显示相同的图像:

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

    if collectionView == self.collectionView2{
        collectionView1.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
    }
}
以下是多个集合视图的完整示例代码:

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)
        }
    }

}
结果:


投影以获取更多信息。

显示图像的俯视图似乎没有必要是一个集合视图,您是否可以仅拥有一个包含
UIImageView
的视图?显示图像的俯视图似乎没有必要是一个集合视图,您是否可以仅拥有一个包含
UIImageView
的视图?谢谢,先生。我真的很高兴你支持我。我在这两个星期里一直在犹豫,我仍然找不到任何答案。谢谢。如果你在下一步继续支持我,我会很高兴。我更新了我的问题。但是,我会先自己尝试。如果我有任何问题,我会在这里发布另一个问题。谢谢。:)很高兴帮助你。:)谢谢,先生。我真的很高兴你支持我。我在这两个星期里一直在犹豫,我仍然找不到任何答案。谢谢。如果你在下一步继续支持我,我会很高兴。我更新了我的问题。但是,我会先自己尝试。如果我有任何问题,我会在这里发布另一个问题。谢谢。:)很高兴帮助你。:)