【问题标题】:how to populate cells in a UItableview with switfyJson from an alamofire post request如何从 alamofire 发布请求中使用 switfyJson 填充 UItableview 中的单元格
【发布时间】:2021-12-17 18:21:59
【问题描述】:

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

视图控制器看起来像这样。

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
    }
}

【问题讨论】:

    标签: swift uitableview uiviewcontroller alamofire swifty-json


    【解决方案1】:

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

    在您的 numberOfRowsInSection 函数处返回 self.myModel.count

    cellForRowAt

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

    【讨论】:

    • self.myModel.append... 行中我收到以下错误“在范围内找不到'self'”,在tableViewScore.reloadData() 行中我收到错误“在范围内找不到'tableViewScore' "
    • 在 tableViewScore.reloadData() 中写入你的 tableviews 名称,同时从 self.myModel.append 中删除 self
    • 然后我收到错误“在范围内找不到'myModel'”并且tableViewScore 是我的tableview 的名称
    • myModel 是在哪里定义的?
    • 我创建了一个名为 myModel 的新文件。 import foundation struct MyModel { let teedate: String? let coursename: String? let golfclubname: String? }
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    相关资源
    最近更新 更多