将图像视图从集合视图传递到新的视图控制器

时间:2015-10-01 19:51:50

标签: ios xcode swift

单击图像时,如何将包含图像集内视图的视图控制器传递给另一个视图控制器?因此,当我单击图像时,它会在另一个视图控制器中打开。

cellForItemAtIndexPath代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell

        cell.imageText.adjustsFontSizeToFitWidth = true

        cell.layer.borderWidth = 0.5
        cell.layer.cornerRadius = 3
        let myColor : UIColor = UIColor(red: 0.0, green: 0.0, blue:0.0, alpha: 0.15)
        cell.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.025)
        cell.layer.borderColor = myColor.CGColor

        let post = self.arrayOfDetails[indexPath.row]
        cell.currentUserLabel.text = post.username
        cell.imageText.text = post.text

        cell.uploadedTimeLabel.text = post.CreatedAt.timeAgo

        cell.imageView.setImageWithUrl(NSURL(string: post.image)!, placeHolderImage: UIImage(named: "Placeholder"))

        return cell
    }

详细说明:

struct Details {
    var username:String!
    var text:String!
    var CreatedAt:NSDate!
    var image:String!
    init(username:String,text:String,CreatedAt:NSDate,image:String){

        self.username = username
        self.text = text
        self.CreatedAt = CreatedAt
        self.image = image
    }
}

didSelectItemAtIndexPath方法:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {
         self.performSegueWithIdentifier("showImage", sender: self)
    }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

根据您使用故事板或以编程方式进行操作,我会以同样的方式进行操作:

在新VC中设置您可以公开访问的属性,然后在prepareForSegue或您的委托方法中单击单元格,将所需信息传递到新VC的属性中,然后在viewDidLoadviewWillAppear等中设置您需要的所有数据。

最简单的解决方案当然是传入imageView,但它可能不是正确的分辨率等。因此,传递在prepareForSeguedidSelectItem中重新创建所需的任何数据方法,并在新VC中使用它