繁体   English   中英

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

[英]Pass image view from collection view to a new view controller

单击图像时,如何将集合视图内部具有图像的视图控制器的数据传递给另一个视图控制器? 因此,当我单击图像时,它将在另一个视图控制器中打开。

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

有什么建议吗?

根据您是使用Storyboard还是以编程方式进行操作,我将以相同的方式进行处理:

在新VC中设置所需的属性以使其可公开访问,然后在prepareForSegue或委托方法中单击它们时单击它们,将所需的信息传递到新VC的属性中,然后在viewDidLoadviewWillAppear等使用该数据设置您需要的所有内容。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM