表视图不使用HTTP请求获取的数据进行更新

时间:2016-12-05 20:29:04

标签: ios swift alamofire

我尝试使用从http请求(JSON)获取的数据填充表格视图,但它无法正常工作。如果我使用静态数据填充表视图,一切正常..代码片段:

import UIKit
import Alamofire
//import SwiftyJSON

class ViewControllerNews : UITableViewController{
@IBOutlet var table: UITableView!

var posts = [FacebookPost]()

override func viewDidLoad() {
    pullFacebookNews()
    //loadSampleNews()
    super.viewDidLoad()

}

func pullFacebookNews() -> Void {


    let params = ["limit":"100", "access_token": myaccesstoken]


   Alamofire.request( "https://graph.facebook.com/<page-id>/posts", parameters: params).responseJSON{ response in

    if let responsejson = response.result.value {
        let JSON = responsejson as! NSDictionary
        let itemsArray: NSArray? = JSON.object(forKey: "data") as? NSArray

        if(itemsArray != nil) {
            for item in itemsArray! {

            let thisitem = item as! NSDictionary
            print(thisitem.object(forKey: "message") as? String)
            print(thisitem.object(forKey:"created_time") as? String)
            let title1 = thisitem.object(forKey: "message") as? String
            let value1=thisitem.object(forKey:"created_time") as? String
            if(title1 != nil && value1 != nil) {
                  let news = FacebookPost(title: title1!, value: value1!)
                    self.posts.append(news)
                }

            }}

        }
    }
 do_table_refresh()
}

func do_table_refresh() {


DispatchQueue.global(qos: .background).async {

DispatchQueue.main.async {
self.table.reloadData()
}
}
}
func loadSampleNews() {
    let news1 = FacebookPost(title: "ich bin ein datum", value: "Ich bin eine news")

    let news2 = FacebookPost(title: "ich bin ein datum2", value: "Ich bin eine news2")

    let news3 = FacebookPost(title: "ich bin ein datum3", value: "Ich bin eine news3")

    posts += [news1, news2, news3]
}

override func numberOfSections(in: UITableView) -> Int {
    return 1
}
override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}
override func tableView(_: UITableView, cellForRowAt: IndexPath) -> UITableViewCell {
    let cellIdentifier = "NewsTableViewCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: cellForRowAt as IndexPath) as! NewsTableViewCell

    let news = posts[cellForRowAt.row]

    cell.date.text = news.title
    cell.news_text.text = news.value

    return cell
}
}

loadSampeNews()工作正常,但pullFacebookNews()不会填充表格。调试数组时已经填充..

1 个答案:

答案 0 :(得分:2)

如果设置了断点,则会在点击do_table_refresh()之前看到if let responsejson = response.result.value被调用。这是因为Alamofire调用有一个完成处理程序。您必须等到互联网电话完成后再拨打do_table_refresh()

因此,如果您的所有数据都按照您的说法正确加载,那么您基本上只需移动do_table_refresh()

func pullFacebookNews() -> Void {
  let params = ["limit":"100", "access_token": myaccesstoken]
  Alamofire.request( "https://graph.facebook.com/<page-id>/posts", parameters: params).responseJSON{ response in

    if let responsejson = response.result.value {
      let JSON = responsejson as! NSDictionary
      let itemsArray: NSArray? = JSON.object(forKey: "data") as? NSArray

      if(itemsArray != nil) {
        for item in itemsArray! {
          let thisitem = item as! NSDictionary
          print(thisitem.object(forKey: "message") as? String)
          print(thisitem.object(forKey:"created_time") as? String)
          let title1 = thisitem.object(forKey: "message") as? String
          let value1=thisitem.object(forKey:"created_time") as? String
          if(title1 != nil && value1 != nil) {
            let news = FacebookPost(title: title1!, value: value1!)
            self.posts.append(news)
          }
        }
      }
      self.do_table_refresh()
    }
  }
}

此外,请勿使用该语法命名方法。这不是最佳做法。应该是doTableRefresh()或其他什么。