Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. struct face {
  2. var name: String
  3. var directLink: String
  4. var image: String
  5. }
  6.  
  7. import UIKit
  8. import SDWebImage
  9. import FontAwesome_swift
  10.  
  11. class TopratedVC: BaseViewController, UICollectionViewDataSource {
  12.  
  13. @IBOutlet var collectionview: UICollectionView!
  14. @IBOutlet var Image: UIImageView!
  15. @IBAction func buttonViewLinkAction(sender: UIButton) {
  16. }
  17.  
  18.  
  19. var faces = [face]()
  20. var placeholderImage = UIImage(named:"heart.png")!
  21.  
  22. //Our web service url
  23. let URL_GET_coffeemugs = "http://coffeemugs.com/feed.php"
  24.  
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. //addSlideMenuButton()
  28. // Do any additional setup after loading the view.
  29.  
  30. //set collection view layout size
  31. let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  32. layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
  33. collectionview.collectionViewLayout = layout
  34.  
  35. //created NSURL
  36. if let requestURL = NSURL(string: URL_GET_coffeemugs) {
  37. //creating NSMutableURLRequest
  38. let request = NSMutableURLRequest(URL: requestURL)
  39. //setting the method to post
  40. request.HTTPMethod = "GET"
  41. //creating a task to send the post request
  42. let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
  43. data, response, error in
  44. //exiting if there is some error
  45. if error != nil{
  46. print("error is \(error)")
  47. return;
  48. }
  49.  
  50. //parsing the response
  51. do {
  52. guard let coffeemugs = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSArray else {
  53. //Doesn't exist, or isn't an NSArray
  54. return
  55. }
  56.  
  57. var newfaces=[face]()
  58.  
  59. for coffeemug in coffeemugs {
  60. //getting the data at each index
  61. let coffeemugName = coffeemug["name"] as! String
  62. let coffeemugDirectLink = coffeemug["direct_link"] as! String
  63. let coffeemugImage = coffeemug["image"] as! String
  64.  
  65. let newface = face(name:coffeemugName,
  66. directLink: coffeemugDirectLink,
  67. image:coffeemugImage)
  68. newfaces.append(newface)
  69. //displaying the data
  70. print("name -> ", coffeemugName)
  71. print("direct_link -> ", coffeemugDirectLink)
  72. print("image -> ", coffeemugImage)
  73. print("===================")
  74. print()
  75.  
  76. }
  77. dispatch_async(dispatch_get_main_queue(),{
  78. self.faces = newfaces
  79. self.collectionview.reloadData()
  80. })
  81.  
  82. } catch {
  83. print(error)
  84. }
  85. }
  86. //executing the task
  87. task.resume()
  88. }
  89. }
  90.  
  91. func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
  92. {
  93. return self.faces.count
  94. }
  95.  
  96. func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  97. let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CellClass
  98. let face = self.faces[indexPath.item]
  99.  
  100. //set image and align center
  101. if let imageURL = NSURL(string:face.image) {
  102. cell.imageView.sd_setImageWithURL(imageURL)
  103.  
  104. } else {
  105. cell.imageView.image = self.placeholderImage
  106. }
  107.  
  108. //set name
  109. if let imageNAME: String = String(face.name){
  110. cell.labelView.text = (imageNAME .uppercaseString)
  111.  
  112. } else {
  113. cell.labelView.text = "oops name"
  114. }
  115.  
  116. //set border
  117. cell.layer.shadowOffset = CGSizeMake(0, 1)
  118. cell.layer.shadowColor = UIColor.blackColor().CGColor
  119. cell.layer.shadowRadius = 1
  120. cell.layer.shadowOpacity = 0.1
  121. cell.clipsToBounds = false
  122. let shadowFrame: CGRect = (cell.layer.bounds)
  123. let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath
  124. cell.layer.shadowPath = shadowPath
  125.  
  126. //square background button
  127. cell.buttonViewSquare.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1)
  128. cell.buttonViewSquare.enabled = false
  129.  
  130. //fontawesome link
  131. cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
  132. cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
  133. cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)
  134.  
  135. func buttonViewLinkAction(sender:UIButton!) {
  136. print("Button tapped")
  137. UIPasteboard.generalPasteboard().string = "Label text"
  138.  
  139. }
  140.  
  141.  
  142. //fontawesome heart
  143. cell.buttonViewHeart.setTitle(String.fontAwesomeIconWithName(.Heart), forState: .Normal)
  144. cell.buttonViewHeart.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
  145. cell.buttonViewHeart.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
  146.  
  147. //fontawesome share
  148. cell.buttonViewShare.setTitle(String.fontAwesomeIconWithName(.Share), forState: .Normal)
  149. cell.buttonViewShare.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
  150.  
  151.  
  152.  
  153.  
  154. return cell
  155. }
  156.  
  157.  
  158. // Create 2 columns
  159. func collectionView(collectionView: UICollectionView,
  160. layout collectionViewLayout: UICollectionViewLayout,
  161. sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
  162. //device screen size
  163. let width = UIScreen.mainScreen().bounds.size.width
  164. //calculation of cell size
  165. return CGSize(width: ((width / 2) - 15) , height: 155)
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement