Last active
March 27, 2016 15:49
-
-
Save gfelot/dd81f7d843391dd1e65a to your computer and use it in GitHub Desktop.
Custom TableViewCell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BookTableViewCell.swift | |
// WIMB | |
// | |
// Created by Gil Felot on 27/03/16. | |
// Copyright © 2016 gfelot. All rights reserved. | |
// | |
import UIKit | |
class BookTableViewCell: UITableViewCell { | |
@IBOutlet weak var view: UIView! | |
@IBOutlet weak var _titre: UILabel! | |
@IBOutlet weak var _desc: UILabel! | |
let nibName: String = "BookTableViewCell" | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
// Initialization code | |
setup() | |
} | |
override func setSelected(selected: Bool, animated: Bool) { | |
super.setSelected(selected, animated: animated) | |
// Configure the view for the selected state | |
} | |
func setup() { | |
view = loadViewFromNib() | |
view.frame = bounds | |
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] | |
addSubview(view) | |
} | |
func loadViewFromNib() -> UIView { | |
let bundle = NSBundle(forClass: self.dynamicType) | |
let nib = UINib(nibName: nibName, bundle: bundle) | |
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView | |
return view | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// BookViewParallaxController.swift | |
// WIMB | |
// | |
// Created by Gil Felot on 25/03/16. | |
// Copyright © 2016 gfelot. All rights reserved. | |
// | |
import UIKit | |
import Parse | |
import Alamofire | |
import PINRemoteImage | |
extension Double { | |
func toString() -> String { | |
return String(format: "%.1f",self) | |
} | |
} | |
class BookViewParallaxController: UITableViewController, ScanBookDelegate { | |
@IBOutlet var tableview: UITableView! | |
var headerView = ParallaxHeaderView() | |
var myBook: BookFromJSON? | |
var myBookFromCloud: BookFromCloud? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
if myBook == nil && myBookFromCloud == nil{ | |
performSegueWithIdentifier("scanCode", sender: nil) | |
} | |
if myBook != nil { | |
fillFromAPI() | |
} else if myBookFromCloud != nil { | |
fillFromCloud() | |
} | |
self.tableview.tableHeaderView = headerView | |
self.tableView.registerNib(UINib(nibName: "BookTableViewCell", bundle: nil), forCellReuseIdentifier: "bookCell") | |
self.tableView.estimatedRowHeight = 68.0 | |
self.tableView.rowHeight = UITableViewAutomaticDimension | |
} | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 4 | |
} | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier("bookCell")! as! BookTableViewCell | |
if indexPath.row == 0 { | |
cell._titre.text = "Titre :" | |
cell._desc.text = myBookFromCloud?.data["title"] as? String | |
} else if indexPath.row == 1 { | |
cell._titre.text = "Auteur :" | |
let authors = myBookFromCloud?.data["authors"] as? [String] | |
cell._desc.text = authors?.first | |
} else if indexPath.row == 2 { | |
cell._titre.text = "Description :" | |
cell._desc.text = myBookFromCloud?.data["desc"] as? String | |
} else if indexPath.row == 3 { | |
cell._titre.text = "Note :" | |
let note:Double = (myBookFromCloud?.data["averageRating"] as? Double)! | |
cell._desc.text = note.toString() | |
} | |
return cell | |
} | |
// override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { | |
// let cell = tableView.dequeueReusableCellWithIdentifier("bookCell")! as UITableViewCell | |
// let height = (cell.textLabel?.frame.height)! + (cell.detailTextLabel?.frame.height)! + 20 | |
// print(cell.detailTextLabel?.frame.height) | |
// return height | |
// } | |
//Set book view controller as delegate to scan book view controller | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
if segue.destinationViewController is ScanBookViewController{ | |
let scanBookViewController:ScanBookViewController = segue.destinationViewController as! ScanBookViewController | |
scanBookViewController.delegate = self | |
} | |
} | |
func fillFromAPI() { | |
if let coverString = myBook?.data["cover"] as! String! { | |
if let url = NSURL(string: coverString) { | |
let imgView = UIImageView() | |
imgView.pin_setImageFromURL(url) | |
headerView = ParallaxHeaderView.parallaxHeaderViewWithImage(imgView.image, forSize: CGSizeMake(self.tableview.frame.size.height, 300)) as! ParallaxHeaderView | |
self.tableview.tableHeaderView = headerView | |
} else { | |
headerView = ParallaxHeaderView.parallaxHeaderViewWithImage(UIImage(named: "No_image"), forSize: CGSizeMake(self.tableview.frame.size.height, 300)) as! ParallaxHeaderView | |
} | |
} else { | |
headerView = ParallaxHeaderView.parallaxHeaderViewWithImage(UIImage(named: "No_image"), forSize: CGSizeMake(self.tableview.frame.size.height, 300)) as! ParallaxHeaderView | |
} | |
} | |
func fillFromCloud() { | |
let imageFile = myBookFromCloud?.data["coverFile"] | |
imageFile?.getDataInBackgroundWithBlock({ (imageData, error) in | |
if error == nil { | |
if let imageData = imageData { | |
self.headerView = ParallaxHeaderView.parallaxHeaderViewWithImage(UIImage(data: imageData), forSize: CGSizeMake(self.tableview.frame.size.height, 300)) as! ParallaxHeaderView | |
self.tableview.tableHeaderView = self.headerView | |
} | |
} | |
}) | |
} | |
@IBAction func saveBookToCloud(sender: AnyObject) { | |
print("Test") | |
if myBook != nil { | |
let book = myBook!.prepareToCloud((headerView.headerImage.images?.first)!) | |
print(book) | |
book.saveInBackgroundWithBlock { | |
(success: Bool, error: NSError?) -> Void in | |
if (success) { | |
self.alertPopUp("Book Saved", message: "It's in the Cloud now") | |
} else { | |
print(error!) | |
} | |
} | |
} | |
} | |
func alertPopUp(title:String, message:String) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in | |
self.navigationController?.popViewControllerAnimated(true) | |
})) | |
self.presentViewController(alert, animated: true, completion: nil) | |
} | |
func didScanBook(scannedBook:BookItems!) | |
{ | |
let bookItems: BookItems! = scannedBook | |
if bookItems != nil { | |
myBook = bookItems.data?.first | |
} | |
} | |
override func scrollViewDidScroll(scrollView: UIScrollView) { | |
let header: ParallaxHeaderView = self.tableview.tableHeaderView as! ParallaxHeaderView | |
header.layoutHeaderViewForScrollViewOffset(scrollView.contentOffset) | |
self.tableview.tableHeaderView = header | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment