[
{
"userId": 1,
"id": 1,
"title": "xxxxx",
"body": "yyyyy"
},
{
我的 json 数据就是这样,我使用 alamofire 加载数据,使用 objectmapper 进行映射。
我为这样的映射创建了一个 swift 文件:
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"]
}
}
我的 View Controller 是这样的:
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)")
}
}
}
}
我尝试将 json 数据打印到 TableView。数据即将到来,但我无法将其添加到 tableview。我应该怎么做才能解决这个问题?
最佳答案
您不需要 TrainSchedules 模型。
您的模型:
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"]
}
}
你的 View Controller :
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
}
}
关于ios - Alamofire 和 Objectmapper 将数据添加到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49321953/