Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. class CategoryRow : UITableViewCell {
  2. @IBOutlet weak var collectionView: UICollectionView!
  3. var getUserData = ["clientType" : "2", "docType" : "002","docNum": "16191725","email": "corp.admin@postpago.com","msisdn": "914171620"]
  4. var purchases : NSArray = []
  5.  
  6. struct MobileLines {
  7. let name:String
  8. let benefit:String
  9. let price:String
  10. let expiration:String
  11.  
  12. init?(_ dict:[String:Any]?) {
  13. guard let _dict = dict,
  14. let name = _dict["name"] as? String,
  15. let benefit = _dict["benefit"] as? String,
  16. let price = _dict["price"] as? String,
  17. let expiration = _dict["expiration"] as? String
  18. else { return nil }
  19.  
  20. self.name = name
  21. self.benefit = benefit
  22. self.price = price
  23. self.expiration = expiration
  24. }
  25. }
  26.  
  27. var arrMobileLines = [MobileLines]()
  28.  
  29. func getPurchases (_ userData: NSDictionary) {
  30. let url = URL(string: "http://162.209.99.39:8080/MiClaroBackend/purchase/mobile/MobilePostpaidActive-51914171620/products?type=voice")
  31. let session = URLSession.shared
  32.  
  33. let request = NSMutableURLRequest(url: url!)
  34. request.httpMethod = "POST"
  35.  
  36. self.getUserData = userData as! [String : String]
  37. let paramToSend = ["userData":userData]
  38. request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  39. request.httpBody = try! JSONSerialization.data(withJSONObject: paramToSend)
  40.  
  41. let task = session.dataTask(with: request as URLRequest, completionHandler: {
  42. (data, response, error) in
  43.  
  44. guard let _:Data = data else
  45. {
  46. return
  47. }
  48.  
  49. let json:AnyObject?
  50.  
  51. do {
  52. json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
  53. if let parseJSON = json{
  54. let purchaseProducts = parseJSON["purchaseProducts"] as! NSArray
  55. self.purchases = purchaseProducts
  56.  
  57. print(json!)
  58. }
  59. } catch {
  60.  
  61. }
  62. })
  63.  
  64. task.resume()
  65. }
  66. }
  67.  
  68. extension CategoryRow : UICollectionViewDataSource {
  69.  
  70. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  71. return self.arrMobileLines.count
  72. }
  73.  
  74. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  75. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
  76. self.arrMobileLines = self.purchases.flatMap({ MobileLines($0 as? [String:Any])})
  77.  
  78. let dataAtIndex = self.arrMobileLines[indexPath.row]
  79.  
  80. cell.name.text = String(dataAtIndex.name)
  81. return cell
  82. }
  83.  
  84. }
  85.  
  86. extension CategoryRow : UICollectionViewDelegateFlowLayout {
  87.  
  88. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  89. let itemsPerRow:CGFloat = 4
  90. let hardCodedPadding:CGFloat = 5
  91. let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
  92. let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
  93. return CGSize(width: itemWidth, height: itemHeight)
  94. }
  95.  
  96. }
  97.  
  98. import UIKit
  99.  
  100. class VideoCell : UICollectionViewCell {
  101.  
  102. @IBOutlet weak var name: UILabel!
  103. }
  104.  
  105. class ViewController: UIViewController {
  106. var categories = ["Action", "Drama", "Science Fiction", "Kids", "Horror"]
  107.  
  108. var categoryRow = CategoryRow()
  109.  
  110. override func viewDidLoad() {
  111. super.viewDidLoad()
  112. categoryRow.getPurchases(categoryRow.getUserData as NSDictionary)
  113.  
  114. }
  115.  
  116. }
  117.  
  118. extension ViewController : UITableViewDelegate { }
  119.  
  120. extension ViewController : UITableViewDataSource {
  121.  
  122. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  123. return categories[section]
  124. }
  125.  
  126. func numberOfSections(in tableView: UITableView) -> Int {
  127. return 1
  128. }
  129.  
  130. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  131. return 1
  132. }
  133.  
  134. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  135. let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
  136. return cell
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement