我有一個嵌入在 Viewcontroller 中的 Tableviewcontroller。我想用我的 alamofire post 請求中的資料填充這 3 個單元格。完成這項作業的最簡單方法是什么?我可以在除錯區看到我的 alamofire post 請求。到目前為止我來了。
視圖控制器看起來像這樣。
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
@IBOutlet weak var tableViewScore: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getScores()
tableViewScore.delegate = self
tableViewScore.dataSource = self
}
}
public func getScores() {
let URL_SCORES = "http://localhost/projecttitle/v1/scores.php"
let username = "[email protected]"
//creating parameters for the post request for now.
let parameters: Parameters=[
"username":username
]
//Sending http post request
AF.request(URL_SCORES, method: .post, parameters: parameters).responseJSON
{
response in
//printing response
print(response)
switch response.result {
case .success (let value):
let json = JSON(value)
for (key,subJson):(String, JSON) in json["scores"] {
debugPrint (key) //key
debugPrint (subJson) //value
debugPrint (subJson["date"]) //value
debugPrint (subJson["coursename"]) //value
debugPrint (subJson["score"]) //value
}
case .failure(let error):
print(error)
}
}
}
extension ViewController : UITableViewDataSource{
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellScores
// populate the cells with date, coursename and score
return cell
}
}
extension UIViewController : UITableViewDelegate{
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}
UITableViewCell 看起來像這樣。
import UIKit
class TableViewCellScores: UITableViewCell {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var courseNameLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
uj5u.com熱心網友回復:
您可以在視圖控制器的外部創建一個名為“MyModel”的模型:
struct MyModel {
let date: String?
let coursename: String?
let score: String?
}
并在您的視圖控制器中定義一個變數:
var myModel = [MyModel]()
在您的getScores()func fill your model:
for (key,subJson):(String, JSON) in json["scores"] {
debugPrint (key) //key
debugPrint (subJson) //value
debugPrint (subJson["date"]) //value
debugPrint (subJson["coursename"]) //value
debugPrint (subJson["score"]) //value
self.myModel.append(date: subJson["date"], coursename: subJson["coursename"], score: subJson["score"])
}
tableViewScore.reloadData() //reload your tableview end of the for loop
回傳self.myModel.count您的numberOfRowsInSection功能
在cellForRowAt:
cell.dateLabel.text = self.myModel[indexPath.row].date
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/351854.html
標籤:迅速 合适的视图 用户界面控制器 阿拉莫火 swifty-json
上一篇:帶有Universal和Ionic的AngularSSR不會在頁面源中顯示實際資料
下一篇:如何選擇移動的范圍
