Skip to content

Instantly share code, notes, and snippets.

@gfelot
Last active March 18, 2016 12:05
Show Gist options
  • Save gfelot/7117ac7855528b7e0832 to your computer and use it in GitHub Desktop.
Save gfelot/7117ac7855528b7e0832 to your computer and use it in GitHub Desktop.
Swift CollectioView
//
// LibraryViewController.swift
// WIMB
//
// Created by Gil Felot on 16/03/16.
// Copyright © 2016 gfelot. All rights reserved.
//
import UIKit
import Parse
let parallaxCellIdentifier = "parallaxCell"
class LibraryViewController: UICollectionViewController {
var bookImages = [String?]()
var bookTitle = [String?]()
var nbBooks = 0
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className: "Book")
query.whereKey("userID", equalTo: (PFUser.currentUser()?.objectId)!)
query.findObjectsInBackgroundWithBlock { (books, error) in
guard (books != nil) else {
return
}
for book in books! {
print("\n\n\(book)")
if let coverString = book["cover"] {
self.bookImages.append(coverString as? String)
} else {
self.bookImages.append(nil)
}
if let titleString = book["title"] {
self.bookTitle.append(titleString as? String)
} else {
self.bookTitle.append("No Title")
}
}
}
let collectionView = self.collectionView
collectionView?.reloadData()
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("NB Book: \(bookTitle.count)")
return bookTitle.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let parallaxCell = collectionView.dequeueReusableCellWithReuseIdentifier(parallaxCellIdentifier, forIndexPath: indexPath) as! ParallaxCollectionViewCell
let imgString = bookImages[indexPath.row]
if imgString != nil {
if let url = NSURL(string: imgString!) {
print("Cover Found ")
parallaxCell.imageView.pin_setImageFromURL(url)
}
}else {
print("Cover Not Found")
parallaxCell.image = UIImage(named: "iu")!
}
return parallaxCell
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
guard let collectionView = self.collectionView else {return}
guard let visibleCells = collectionView.visibleCells() as? [ParallaxCollectionViewCell] else {return}
for parallaxCell in visibleCells {
let yOffset = ((collectionView.contentOffset.y - parallaxCell.frame.origin.y) / ImageHeight) * OffsetSpeed
parallaxCell.offset(CGPointMake(0.0, yOffset))
}
}
@IBAction func logout(sender: AnyObject) {
// PFUser.logOut()
// self.performSegueWithIdentifier("logout", sender: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment