Alamofire和Objectmapper将数据添加到tableview

编程入门 行业动态 更新时间:2024-10-25 08:27:14
本文介绍了Alamofire和Objectmapper将数据添加到tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 [ { "userId": 1, "id": 1, "title": "xxxxx", "body": "yyyyy" }, {

我的json数据是这样的,我正在使用alamofire加载数据和objectmapper进行映射。

My json data is like that and I'm using alamofire for loading data and objectmapper for mapping.

我创建一个swift文件进行映射:

I create a swift file for mapping like that:

import Foundation import ObjectMapper class TrainSchedules: Mappable { var mySchedules: [Schedules] required init?(map: Map) { mySchedules = [] } func mapping(map: Map) { mySchedules <- map["schedule"] } } class Schedules: Mappable { var userId: String var id: String var title: String var body: String required init?(map: Map) { userId = "" id = "" title = "" body = "" } func mapping(map: Map) { userId <- map["userId"] id <- map["id"] title <- map["title"] body <- map["body"] } }

和我的视图控制器一样:

and my view controller like that:

import Alamofire import ObjectMapper class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() cell.textLabel?.text = "Landmark" return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } @IBOutlet weak var tableViewData: UITableView! override func viewDidLoad() { super.viewDidLoad() tableViewData.dataSource = self tableViewData.delegate = self let jsonDataUrl = "jsonplaceholder.typicode/posts" Alamofire.request(jsonDataUrl).responseJSON { response in print("Request: \(String(describing: response.request))") print("Response: \(String(describing: response.response))") print("Result: \(response.result)") if let json = response.result.value { print("JSON: \(json)") } if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) { print("Data: \(utf8Text)") } } } }

I试图将json数据打印到TableView.Data即将到来,但我不能dd it to tableview。我该怎么做才能解决这个问题?

I tried to print json data to TableView.Data is coming however I couldn't add it to tableview.What should I do to solve this problem ?

推荐答案

你不需要TrainSchedules模型。

You don't need TrainSchedules model.

您的型号:

import Foundation import ObjectMapper class Schedule: Mappable { var userId: String var id: String var title: String var body: String required init?(map: Map) { userId = "" id = "" title = "" body = "" } func mapping(map: Map) { userId <- map["userId"] id <- map["id"] title <- map["title"] body <- map["body"] } }

你的ViewController:

Your ViewController:

import Alamofire import ObjectMapper import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableViewData: UITableView! var schedules: [Schedule]? override func viewDidLoad() { super.viewDidLoad() tableViewData.dataSource = self tableViewData.delegate = self loadData() } func loadData() { let jsonDataUrl = "jsonplaceholder.typicode/posts" Alamofire.request(jsonDataUrl).responseJSON { response in self.schedules = Mapper<Schedule>().mapArray(JSONObject: response.result.value) self.tableViewData.reloadData() } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() cell.textLabel?.text = schedules?[indexPath.row].title return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return schedules?.count ?? 0 } }

更多推荐

Alamofire和Objectmapper将数据添加到tableview

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

发布评论

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

>www.elefans.com

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