如何使用来自 alamofire post 请求的 switfyJson 填充 UItableview 中的单元格

how to populate cells in a UItableview with switfyJson from an alamofire post request

我在 Viewcontroller 中嵌入了一个表 viewcontroller。我想用我的 alamofire post 请求中的数据填充这 3 个单元格。完成这项工作最简单的方法是什么?我可以在调试区域看到我的 alamofire post 请求。到此为止。

viewcontroller 看起来像这样。

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 = "username@projecttitle.com"
        
        //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
    }
}

您可以在视图控制器外部创建一个名为“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 

Return self.myModel.count 在你的 numberOfRowsInSection func

cellForRowAt

cell.dateLabel.text = self.myModel[indexPath.row].date