使用SwiftyJSON和Alamofire在TableView上显示JSON数据

编程入门 行业动态 更新时间:2024-10-13 16:15:10
本文介绍了使用SwiftyJSON和Alamofire在TableView上显示JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在表视图上显示从服务器获取的JSON数据.问题是,我无法让它显示在上面.我尝试了几种不同的方法,并进行了大量搜索以找到解决方案,但我做不到.

I want to show the JSON data grabbed from a server on a Table View. The problem is, I can't get it to show up on it. I have tried several different methods and searched a lot to find a solution, but I can't.

我的代码(所有代码)如下所示,希望有人能帮助我.预先感谢.

My code (all of it) is shown below and I hope somebody can help me out. Thanks in advance.

import UIKit import Alamofire import SwiftyJSON class MasterViewController: UITableViewController { var tableTitle = [String]() var tableBody = [String]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. getJSON() } func getJSON(){ Alamofire.request(.GET, "announcement.vassy/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in // checking if result has value if let value = Response.result.value { let json = JSON(value) for anItem in json.array! { let title: String? = anItem["Title"].stringValue let body: String? = anItem["Body"].stringValue self.tableTitle.append(title!) self.tableBody.append(body!) } } } self.tableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // Table View Stuff override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.tableTitle.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell // cell config cell.title!.text = tableTitle[indexPath.row] cell.body!.text = tableBody[indexPath.row] return cell } }

推荐答案

Alamofire网络请求是异步的,这意味着您不知道何时返回结果.

The Alamofire network request is asynchronous, meaning you can't know when the result will come back.

这里的问题是您将tableView 重新加载到Alamofire请求的范围之外,因此它在数据返回之前执行.

The problem here is that you reload the tableView outside the scope of the Alamofire request, so it is executed before the data comes back.

重新加载应该在相同的作用域内并且在主线程上进行,例如:

The reload should happen in the same scope, and on the main thread, for example:

func getJSON(){ Alamofire.request(.GET, "announcement.vassy/api/AnnouncementAPI/Get/").responseJSON { (Response) -> Void in // checking if result has value if let value = Response.result.value { let json = JSON(value) for anItem in json.array! { let title: String? = anItem["Title"].stringValue let body: String? = anItem["Body"].stringValue self.tableTitle.append(title!) self.tableBody.append(body!) } dispatch_async(dispatch_get_main_queue()) { self.tableView.reloadData() } } } }

更多推荐

使用SwiftyJSON和Alamofire在TableView上显示JSON数据

本文发布于:2023-11-26 02:30:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632293.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Alamofire   SwiftyJSON   JSON   TableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!