2016-09-28 37 views
0

我試圖讓swfityjson和阿爾莫在Xcode 8 工作就像郵局在此頁:Swift3 Alamofire SwiftyJSON得到外界的數據founcion

Showing JSON data on TableView using SwiftyJSON and Alamofire

但我不能得到的數據自。 這幫了我一些,但我堅持與伯爵。有人請幫助我,因爲我是新來的迅速,並花了一天的時間,試圖瞭解如何獲得行數和數據出almo函數。

import UIKit 
import Alamofire 
import SwiftyJSON 

class TableViewController: UITableViewController { 

var names = [String]() 
var rowCount = [Int]() 
var tableTitle = [String]() 
typealias JSONStandard = [String : AnyObject] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

} 


func callAlamo(){ 
    let url = "http://xxxxxx/xxxx/xxxxxx.php" 
    //let id = UserDefaults.standard.string(forKey: "UserDefaults") 
    Alamofire.request(url).responseJSON{(respones)->Void in 

     if let value = respones.result.value{ 
      let json = JSON(value) 
      let rows = json["items"].arrayValue 
      for anItem in rows { 
       let title: String? = anItem["SupplierName"].stringValue 
       self.tableTitle.append(title!) 
       //print(self.tableTitle.count) 
      } 

     } 


    } 
} 

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

override func viewDidAppear(_ animated: Bool) { 
    // let isUserLoggedin = UserDefaults.standard.boll(forKey: "userLogIn") 
    let ststus = UserDefaults.standard.string(forKey: "userLogIn") 
    if ststus == "false" { 
     self.performSegue(withIdentifier: "loginView", sender: self) 
    }else{ 

     callAlamo() 
    } 
} 



@IBAction func logout(_ sender: AnyObject) { 
    UserDefaults.standard.set(false, forKey: "userLogIn") 
    UserDefaults.standard.synchronize() 
    self.performSegue(withIdentifier: "loginView", sender: self) 
} 


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print (tableTitle) 
    return self.tableTitle.count 
} 

}

我無法獲得tableTitle.count。謝謝!

+0

重新加載tableview中後得到的迴應 – Ragul

回答

0

UITableView委託方法在viewDidAppear之前被調用。所以,你需要重新加載的UITableView了從服務器

DispatchQueue.main.async { 
      self.tableView.reloadData()    
     } 

響應後for循環之後。

+0

由於我沒有加入到循環的結束並沒有改變 –

+0

它不會工作? – Ragul

+0

這在Xcode中不起作用。 「預期的聲明」 –

-1
import UIKit 
import Alamofire 
import SwiftyJSON 

class TableViewController: UITableViewController { 

var names = [String]() 
var rowCount = [Int]() 
var tableTitle = [String]() 
typealias JSONStandard = [String : AnyObject] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

} 


func callAlamo(){ 
    let url = "http://pos1.dermedia.co.il/iphone/getLasttransactions.php?cardid=" 
    //let id = UserDefaults.standard.string(forKey: "UserDefaults") 
    Alamofire.request(url).responseJSON{(respones)->Void in 

     if let value = respones.result.value{ 
      let json = JSON(value) 
      // print (json["items"].arrayValue) 
      let rows = json["items"].arrayValue 
      // print (rows) 
      for anItem in rows { 
       let title: String? = anItem["SupplierName"].stringValue 
       self.tableTitle.append(title!) 
       print(self.tableTitle.count) 
      } 

     } 
     DispatchQueue.main.async { 
      self.tableView.reloadData() 
     } 

    } 

} 

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

override func viewDidAppear(_ animated: Bool) { 
    // let isUserLoggedin = UserDefaults.standard.boll(forKey: "userLogIn") 
    let ststus = UserDefaults.standard.string(forKey: "userLogIn") 
    if ststus == "false" { 
     self.performSegue(withIdentifier: "loginView", sender: self) 
    }else{ 

     callAlamo() 
     print ("gggggg") 
     print(self.tableTitle.count) 
    } 
} 



    @IBAction func logout(_ sender: AnyObject) { 
    UserDefaults.standard.set(false, forKey: "userLogIn") 
    UserDefaults.standard.synchronize() 
    self.performSegue(withIdentifier: "loginView", sender: self) 

} 


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print (self.tableTitle) 
    print("hhh") 
    return tableTitle.count 
} 





} 
+0

同樣我已經回答 – Ragul

+0

Ragul感謝您的幫助。但在功能tableView我不明白它仍然是「0」 –

相關問題