Alamofire和Objectmapper将数据添加到tableview [英] Alamofire and Objectmapper adding data to tableview
本文介绍了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 = "https://jsonplaceholder.typicode.com/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 = "https://jsonplaceholder.typicode.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文