【问题标题】:Pass image view from collection view to a new view controller将图像视图从集合视图传递到新的视图控制器
【发布时间】:2015-10-02 04:30:02
【问题描述】:

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

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

有什么建议吗?

【问题讨论】:

  • 你的 didSelectItem 方法在哪里?

标签: ios xcode swift


【解决方案1】:

根据您是使用 Storyboards 还是以编程方式进行,我会以同样的方式进行:

在您的新 VC 中设置您需要的可公开访问的属性,然后在 prepareForSegue 或您的委托方法中设置当他们单击单元格时,将您想要的信息传递到新 VC 的属性中,然后在viewDidLoadviewWillAppear 等中,使用该数据设置您需要的一切。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多