UICollectionView to DetailView Swift, using parse



我有一个UICollectionview,它从Parse中获取图像。

我现在正在尝试推送到详细视图,然后在UICollectionView中推送图像。

有人知道怎么做吗?

这是我的代码:

视图控制器:

       import UIKit
    import Parse
    class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
        @IBOutlet var collectionView: UICollectionView?
        var imageFilesArray:NSArray = NSArray()
        var refreshControl: UIRefreshControl!
      //  var isAscending = true
        override func viewDidLoad() {
            super.viewDidLoad()
            self.refreshControl = UIRefreshControl()
            self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
            self.refreshControl.addTarget(self, action: Selector("imageRefresh"), forControlEvents: UIControlEvents.ValueChanged)
            self.refreshControl.tintColor = UIColor.whiteColor()
            self.collectionView?.addSubview(refreshControl)
            // Do any additional setup after loading the view, typically from a nib.
            //let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            //layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 20, right: 10)
            //layout.itemSize = CGSize(width: 150 , height: 150)
        //    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
            collectionView!.dataSource = self
            collectionView!.delegate = self
            collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "imageCell")
            collectionView?.alwaysBounceVertical = true
            //collectionView!.backgroundColor = UIColor.whiteColor()
            self.view.addSubview(collectionView!)
            parseQuery()

        }
func parseQuery() {
        //query
        var query = PFQuery(className: "photo")
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock{
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {
                self.imageFilesArray = objects
                //println(self.imageFilesArray)
            }
            self.collectionView?.reloadData()
        }
    }

    func imageRefresh() {
        parseQuery()
        self.refreshControl.endRefreshing()
    }
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        println(self.imageFilesArray.count)
        return self.imageFilesArray.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as CollectionViewCell
        cell.backgroundColor = UIColor.blackColor()
         let imageObject = self.imageFilesArray.objectAtIndex(indexPath.row) as PFObject
         let imageFile  = imageObject.objectForKey("imageFile") as PFFile
        imageFile.getDataInBackgroundWithBlock{
            (data: NSData!, error: NSError!) -> Void in
            if error == nil {
                cell.imageView.image = UIImage(data: data)
            }
        }
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

collectionviewcell:

import UIKit
class CollectionViewCell: UICollectionViewCell {
    @IBOutlet var imageView: UIImageView!
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
        imageView.contentMode = UIViewContentMode.ScaleToFill
        contentView.addSubview(imageView)

    }
}

Detailview控制器:

import UIKit
import Parse
class photoDetailViewController: UIViewController {
    @IBOutlet var detailImageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

我希望有人能在如何编写代码以推送到详细视图,并显示被按下的相同图像方面提供帮助。

--OlePeter

更新您的didSelectItemAtIndexPath方法,

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
    var detailsViewController: DetailsViewController = DetailsViewController(nibName:"DetailsViewController",bundle:nil)
    self.presentViewController(detailsViewController, animated: true) { () -> Void in
    }
}

注意:在DetailsViewController的位置写下您自己的DetailsViewController的名称。并编写您自己的nibName

最新更新