我有一个UICollectionview,从Parse获取图像。
我现在正试图推送到详细视图,然后我在UICollectionView中推送图像。
有人知道如何做到这一点吗?
这是我的代码:
的ViewController:
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)
}
}
Detailviewcontroller:
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.
}
*/
}
我希望有人可以帮忙解决如何编写代码以推送到详细视图,并显示被按下的相同图像。
- OlePetter
答案 0 :(得分:0)
更新 didSelectItemAtIndexPath 方法,
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
var detailsViewController: DetailsViewController = DetailsViewController(nibName:"DetailsViewController",bundle:nil)
self.presentViewController(detailsViewController, animated: true) { () -> Void in
}
}
注意:代替 DetailsViewController 编写您自己的详细信息ViewController的名称。并写下你自己的 nibName 。