Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import UIKit
  2. import Alamofire
  3. //import SwiftyJSON
  4.  
  5. class ViewControllerNews : UITableViewController{
  6. @IBOutlet var table: UITableView!
  7.  
  8. var posts = [FacebookPost]()
  9.  
  10. override func viewDidLoad() {
  11. pullFacebookNews()
  12. //loadSampleNews()
  13. super.viewDidLoad()
  14.  
  15. }
  16.  
  17. func pullFacebookNews() -> Void {
  18.  
  19.  
  20. let params = ["limit":"100", "access_token": myaccesstoken]
  21.  
  22.  
  23. Alamofire.request( "https://graph.facebook.com/<page-id>/posts", parameters: params).responseJSON{ response in
  24.  
  25. if let responsejson = response.result.value {
  26. let JSON = responsejson as! NSDictionary
  27. let itemsArray: NSArray? = JSON.object(forKey: "data") as? NSArray
  28.  
  29. if(itemsArray != nil) {
  30. for item in itemsArray! {
  31.  
  32. let thisitem = item as! NSDictionary
  33. print(thisitem.object(forKey: "message") as? String)
  34. print(thisitem.object(forKey:"created_time") as? String)
  35. let title1 = thisitem.object(forKey: "message") as? String
  36. let value1=thisitem.object(forKey:"created_time") as? String
  37. if(title1 != nil && value1 != nil) {
  38. let news = FacebookPost(title: title1!, value: value1!)
  39. self.posts.append(news)
  40. }
  41.  
  42. }}
  43.  
  44. }
  45. }
  46. do_table_refresh()
  47. }
  48.  
  49. func do_table_refresh() {
  50.  
  51.  
  52. DispatchQueue.global(qos: .background).async {
  53.  
  54. DispatchQueue.main.async {
  55. self.table.reloadData()
  56. }
  57. }
  58. }
  59. func loadSampleNews() {
  60. let news1 = FacebookPost(title: "ich bin ein datum", value: "Ich bin eine news")
  61.  
  62. let news2 = FacebookPost(title: "ich bin ein datum2", value: "Ich bin eine news2")
  63.  
  64. let news3 = FacebookPost(title: "ich bin ein datum3", value: "Ich bin eine news3")
  65.  
  66. posts += [news1, news2, news3]
  67. }
  68.  
  69. override func numberOfSections(in: UITableView) -> Int {
  70. return 1
  71. }
  72. override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  73. return posts.count
  74. }
  75. override func tableView(_: UITableView, cellForRowAt: IndexPath) -> UITableViewCell {
  76. let cellIdentifier = "NewsTableViewCell"
  77. let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: cellForRowAt as IndexPath) as! NewsTableViewCell
  78.  
  79. let news = posts[cellForRowAt.row]
  80.  
  81. cell.date.text = news.title
  82. cell.news_text.text = news.value
  83.  
  84. return cell
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement