Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct face {
- var name: String
- var directLink: String
- var image: String
- }
- import UIKit
- import SDWebImage
- import FontAwesome_swift
- class TopratedVC: BaseViewController, UICollectionViewDataSource {
- @IBOutlet var collectionview: UICollectionView!
- @IBOutlet var Image: UIImageView!
- @IBAction func buttonViewLinkAction(sender: UIButton) {
- }
- var faces = [face]()
- var placeholderImage = UIImage(named:"heart.png")!
- //Our web service url
- let URL_GET_coffeemugs = "http://coffeemugs.com/feed.php"
- override func viewDidLoad() {
- super.viewDidLoad()
- //addSlideMenuButton()
- // Do any additional setup after loading the view.
- //set collection view layout size
- let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
- layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
- collectionview.collectionViewLayout = layout
- //created NSURL
- if let requestURL = NSURL(string: URL_GET_coffeemugs) {
- //creating NSMutableURLRequest
- let request = NSMutableURLRequest(URL: requestURL)
- //setting the method to post
- request.HTTPMethod = "GET"
- //creating a task to send the post request
- let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
- data, response, error in
- //exiting if there is some error
- if error != nil{
- print("error is \(error)")
- return;
- }
- //parsing the response
- do {
- guard let coffeemugs = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray else {
- //Doesn't exist, or isn't an NSArray
- return
- }
- var newfaces=[face]()
- for coffeemug in coffeemugs {
- //getting the data at each index
- let coffeemugName = coffeemug["name"] as! String
- let coffeemugDirectLink = coffeemug["direct_link"] as! String
- let coffeemugImage = coffeemug["image"] as! String
- let newface = face(name:coffeemugName,
- directLink: coffeemugDirectLink,
- image:coffeemugImage)
- newfaces.append(newface)
- //displaying the data
- print("name -> ", coffeemugName)
- print("direct_link -> ", coffeemugDirectLink)
- print("image -> ", coffeemugImage)
- print("===================")
- print()
- }
- dispatch_async(dispatch_get_main_queue(),{
- self.faces = newfaces
- self.collectionview.reloadData()
- })
- } catch {
- print(error)
- }
- }
- //executing the task
- task.resume()
- }
- }
- func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
- {
- return self.faces.count
- }
- func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CellClass
- let face = self.faces[indexPath.item]
- //set image and align center
- if let imageURL = NSURL(string:face.image) {
- cell.imageView.sd_setImageWithURL(imageURL)
- } else {
- cell.imageView.image = self.placeholderImage
- }
- //set name
- if let imageNAME: String = String(face.name){
- cell.labelView.text = (imageNAME .uppercaseString)
- } else {
- cell.labelView.text = "oops name"
- }
- //set border
- cell.layer.shadowOffset = CGSizeMake(0, 1)
- cell.layer.shadowColor = UIColor.blackColor().CGColor
- cell.layer.shadowRadius = 1
- cell.layer.shadowOpacity = 0.1
- cell.clipsToBounds = false
- let shadowFrame: CGRect = (cell.layer.bounds)
- let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath
- cell.layer.shadowPath = shadowPath
- //square background button
- cell.buttonViewSquare.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1)
- cell.buttonViewSquare.enabled = false
- //fontawesome link
- cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
- cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
- cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)
- func buttonViewLinkAction(sender:UIButton!) {
- print("Button tapped")
- UIPasteboard.generalPasteboard().string = "Label text"
- }
- //fontawesome heart
- cell.buttonViewHeart.setTitle(String.fontAwesomeIconWithName(.Heart), forState: .Normal)
- cell.buttonViewHeart.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
- cell.buttonViewHeart.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
- //fontawesome share
- cell.buttonViewShare.setTitle(String.fontAwesomeIconWithName(.Share), forState: .Normal)
- cell.buttonViewShare.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
- return cell
- }
- // Create 2 columns
- func collectionView(collectionView: UICollectionView,
- layout collectionViewLayout: UICollectionViewLayout,
- sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
- //device screen size
- let width = UIScreen.mainScreen().bounds.size.width
- //calculation of cell size
- return CGSize(width: ((width / 2) - 15) , height: 155)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement