Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. class DashboardTableViewController: UITableViewController {
  2.  
  3. var userData = [String]()
  4. var secondUserData = [String]()
  5. var name: String = ""
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9. }
  10.  
  11. override func viewWillAppear(_ animated: Bool) {
  12. super.viewWillAppear(animated)
  13. }
  14.  
  15. override func didReceiveMemoryWarning() {
  16. super.didReceiveMemoryWarning()
  17. // Dispose of any resources that can be recreated.
  18. }
  19.  
  20. /* This function gets the user account information from a JSON in a simple request */
  21.  
  22. private func loadUserData(completion: @escaping (_ myArray: [String]) -> Void) {
  23. let session = Twitter.sharedInstance().sessionStore.session()
  24. let client = TWTRAPIClient.withCurrentUser()
  25. let userInfoURL = "https://api.twitter.com/1.1/users/show.json"
  26. let params = ["user_id": session?.userID]
  27. var clientError : NSError?
  28. let request = client.urlRequest(withMethod: "GET", url: userInfoURL, parameters: params, error: &clientError)
  29. client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
  30. if connectionError != nil {
  31. print("Error: (connectionError)")
  32. }
  33.  
  34. do {
  35. let json = JSON(data: data!)
  36. if let userName = json["name"].string,
  37. let description = json["description"].string,
  38. let followersCount = json["followers_count"].int,
  39. let favouritesCount = json["favourites_count"].int,
  40. let followingCount = json["friends_count"].int,
  41. let lang = json["lang"].string,
  42. let nickname = json["screen_name"].string {
  43.  
  44. self.userData.append(userName)
  45. self.userData.append(nickname)
  46. self.userData.append(String(followersCount))
  47. self.userData.append(String(followingCount))
  48. self.userData.append(String("22"))
  49. self.userData.append(lang)
  50. self.userData.append(description)
  51. self.userData.append("No country")
  52.  
  53. completion(self.userData)
  54. }
  55. }
  56. }
  57. }
  58.  
  59. /* This closure helps us to fill the labels once the request has been finished in loadUserData */
  60.  
  61. func manageUserData(label: UILabel, index: Int) {
  62. loadUserData {
  63. (result: [String]) in
  64. label.text = result[index]
  65. }
  66. }
  67.  
  68. // MARK: - Table view data source
  69.  
  70. override func numberOfSections(in tableView: UITableView) -> Int {
  71. // #warning Incomplete implementation, return the number of sections
  72. return 1
  73. }
  74.  
  75. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  76. // #warning Incomplete implementation, return the number of rows
  77. return self.userData.count
  78. }
  79.  
  80. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  81. let rowNumber = indexPath.row
  82. let cellIdentifier = "TableViewCell"
  83. guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TableViewCellController else {
  84. fatalError("The dequeued cell is not an instance of TableViewCellController.")
  85. }
  86.  
  87. switch rowNumber {
  88. case 0:
  89. cell.titlePlaceholder.text = "Name:"
  90. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  91. case 1:
  92. cell.titlePlaceholder.text = "Nickname:"
  93. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  94. case 2:
  95. cell.titlePlaceholder.text = "Followers:"
  96. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  97. case 3:
  98. cell.titlePlaceholder.text = "Following:"
  99. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  100. case 4:
  101. cell.titlePlaceholder.text = "Tweets:"
  102. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  103. case 5:
  104. cell.titlePlaceholder.text = "Language:"
  105. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  106. case 6:
  107. cell.titlePlaceholder.text = "Biography:"
  108. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  109. case 7:
  110. cell.titlePlaceholder.text = "Country:"
  111. manageUserData(label: cell.valuePlaceholder, index: rowNumber)
  112. default:
  113. cell.titlePlaceholder.text = "?"
  114. cell.valuePlaceholder.text = "?"
  115. }
  116.  
  117. return cell
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement