2016-11-06 113 views
0

在我的iOS应用程序中,我使用Alamofire作为休息请求,并使用SwiftyJSON进行分析。其余的网址正在工作,我从服务器获取所有数据,我将其打印在控制台中。但我无法用UITableView填充这些数据。我没有得到任何错误,项目编译和运行没有任何问题,但表视图是空的。这是我简单的代码:使用Alamofire + SwiftyJSON将数据加载到UITableView中

var contracts = [Contract]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    getContracts(contractSearchCriteria: ContractSearchCriteria()) 

} 

// MARK: - Table view data source 

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

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if (contracts.count != 0) { 
     return contracts.count 
    } 
    return 0 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "contractCell", for: indexPath) 

    let contract = self.contracts[indexPath.row] 

    cell.textLabel?.text = contract.insuredPersonFullName 
    cell.detailTextLabel?.text = contract.companyName 

    return cell 
} 

这是getContracts方法:

func getContracts(contractSearchCriteria : ContractSearchCriteria) { 
    let params : Parameters = ["insuredPersonName": contractSearchCriteria.insuredPersonName] 

    Alamofire.request("\(Constants.restURL)getContracts", method: .post, parameters: params, encoding: JSONEncoding.default).validate().responseJSON(completionHandler: {response in 

     switch (response.result) { 
     case .success(let value): 
      let json = JSON(value) 
      let result = Result(json: json) 
      print(result.isSuccess) 

      for data in json["data"].arrayValue { 
       print(data) 
       self.contracts.append(Contract(json: data)) 
      } 
      self.tableView.reloadData() 
     case .failure(let error): 
      print(error) 
     } 


    }) 

} 

我使用SWIFT 3,和最新的Alamofire与SwiftyJSON在Xcode 8.什么是错在我的代码?我找不到任何解决方案。

回答

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

tnx。 soooo愚蠢的错误:) –

相关问题