我想從堆棧交換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
}`
你是否將委託和數據源設置爲tableview?如果是比獲得數據後重新加載tableview –
非常感謝你,重新加載表視圖做了工作 –