【问题标题】:View image in a seperate View Controller from UICollectionView Swift 3从 UICollectionView Swift 3 在单独的视图控制器中查看图像
【发布时间】:2017-01-18 05:32:18
【问题描述】:

所以我正在创建一个 UiCollectionView。您可以在集合视图中滚动浏览一组图像,并且每当您点击其中一个图像时,它将带您进入一个单独的视图控制器并显示您从 UICollectionView 中点击的图像。我看过教程并一步一步地跟着,但我不知道我做错了什么。我可以查看集合视图并滚动它,但只要我点击其中一张图像,它就不会转到单独的视图控制器。我检查了我的课程和标识符。我都说对了。

collectionView 是一个名为 collectionView 的插座

collectionViewCell中的UIimage是一个叫

的出口

collectionViewCell 标识符是 “Cell”

collectionViewCell 类是 OurCell

将保存图像的viewController 称为secondViewController

这是ViewController类文件的代码:

@IBOutlet weak var collectionView: UICollectionView!

let imageArray = [UIImage(named: "EliHouse1" ), UIImage(named: "EliHouse2"), UIImage(named: "EliHouse3" ), UIImage(named: "iPod" )]

func numberOfSections(in collectionView: UICollectionView) -> Int {

    return 1

}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {



    return self.imageArray.count


}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! OurCell


    cell.ourImage?.image = self.imageArray[indexPath.row]



    return cell

}




func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "showImage"
    {
        let indexPaths = self.collectionView!.indexPathsForSelectedItems!
        let indexPath = indexPaths[0] as NSIndexPath
        let vc = segue.destination as! SecondViewController
        vc.image = self.imageArray[indexPath.row]!

    }
}

这是OurCell类文件的代码

@IBOutlet weak var ourImage: UIImageView!

这是SecondViewController类文件的代码

@IBOutlet weak var imageView: UIImageView!
var image = UIImage()

override func viewDidLoad() {
    super.viewDidLoad()


    self.imageView.image = self.image

    // Do any additional setup after loading the view.
}

我在代码中运行它,我可以查看和滚动单元格,但是每当我点击图像时,它不会将我带到 SecondViewController 并且没有任何反应。有人有什么想法吗?我究竟做错了什么?

【问题讨论】:

  • 你在哪里定义showImage segue 标识符?你能把你的故事板也给我们看看吗?
  • 首先不需要输入convert IndexPath instance to NSIndexPath你可以直接使用IndexPath,同样在storyboard你是否创建了从UICollectionViewCell到@987654344的segue @?

标签: ios swift xcode uicollectionview


【解决方案1】:

在您的 collectionView 类中,您应该添加如下内容:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let identifier = "showImage"
    if segue.identifier == identifier {
        let indexPath = collectionView.indexPathsForSelectedItems
        if let indexPath = indexPath?.first,
            let cell = collectionView(rssCollectionView, cellForItemAt: indexPath) as? OurCell,
            let image = cell.ourImage (or something like this to retrieve the picture) {
            let showImage = segue.destination as? SecondViewController
            showImage?.image = image
        }
    }

此外,在情节提要中,您需要使用命令+将 collectionView 单元格拖动到另一个您的 secondViewController 并选择具有所需种类的 segue,然后选择 segue 行并在标识符文本字段中添加“showImage”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    相关资源
    最近更新 更多