2016-03-18 161 views
0

我想从堆栈交换api中使用alamofire和swifty json获取一些数据。我能够在日志中打印所需的数据,但是当我运行应用程序时,模拟器只显示空白单元格。我检查了标识符,并将原型单元格值设置为1.#表格视图不显示内容在单元格中

class MainTableViewController: UITableViewController,UISearchResultsUpdating { 


    var searchKeyword: String = "questions" 

    // empty array to store the search results 
    var searchResults: [SearchResult] = [] 

func alamofireFunction() { 
    Alamofire.request(.GET, "https://api.stackexchange.com/2.2/questions?=%20\(searchKeyword)%20viewpage=1&fromdate=1183075200&todate=2554416000&order=asc&sort=activity&tagged=ios&site=stackoverflow").responseJSON { (response) -> Void in 

      switch response.result { 

      case .Success: 
       if let value = response.result.value { 
        let json = JSON(value) 
        for (var idx=0; idx<=json["items"].count; idx++) { 
         let result = SearchResult() 
         //print(json["items"][idx]["title"].stringValue) 
         result.name = json["items"][idx]["owner"]["display_name"].stringValue 
         result.question = json["items"][idx]["title"].stringValue 
         result.image = json["items"][idx]["owner"]["profile_image"].stringValue 
         self.searchResults.append(result)  
        }     
       } 

      case .Failure: 
       print("error") 



      } 
     } 


    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 


     alamofireFunction() 

} 
     override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    // MARK: - Table view data source 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MainTableViewCell 


     cell.questionLabel.text = searchResults[indexPath.row].question 
     cell.nameLabel.text = searchResults[indexPath.row].name 


     return cell 
    } 



     override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      // #warning Incomplete implementation, return the number of sections 
      return 1 
     } 

     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      // #warning Incomplete implementation, return the number of rows 
      return searchResults.count 

    }` 
+0

你是否将委托和数据源设置为tableview?如果是比获得数据后重新加载tableview –

+1

非常感谢你,重新加载表视图做了工作 –

回答

2

当数据源更改时,您必须调用self.reloadData()。这将导致tableView调用dataSource方法。

0

这是因为您使用的URL的请求,这个过程中执行一个线程,所以当你以这种方式获取你的数据,你必须调用tableView.reloadData()让知道这个过程已经完成

数据源
相关问题