2017-01-17 173 views
-6

所以我試圖獲取一些數據,當視圖控制器加載並設置它,所以我可以稍後在代碼中使用它,但該值不會更新。 CountData值應3在代碼的底部,但它不是Swift 3設置全局變量值

import UIKit 
import Alamofire 

class FooController: UIViewController, UITableViewDataSource { 

    var CountData 

    override func viewDidLoad() { 

     super.viewDidLoad(); 

     Alamofire.request("http://foo:8080/joke.php").responseJSON{ response in 
      if let JSON = response.result.value as? [String: String] { 

      } 
      //!!SET DATA HERE!! 
      self.CountData = 3 
     } 

     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     //!!USE IT HERE!! 
      return CountData; 
} 
+0

self.CountData = 3後,該行重載的tableview再次 –

回答

0

由於Alamofire請求是異步,你需要調用reloadData爲表格。

現在,就可以初始化你

var CountData = 3 

,當你獲得響應設置CountData你想,然後調用

self.CountData = 5  
yourTableView.reloadData() 
+1

最好不要硬編碼計數?當你知道有多少數據已被返回時設置計數,然後重新加載tableView – Russell