首页 > 解决方案 > 从 JSON 响应填充 tableview 部分和行

问题描述

我有一个tableView带有日期的部分。我这样做是这样的:

struct Sections {
    var sectionDate : String!
    var sectionMatches : [Matches2]!
}
struct Matches2 {
    var team1img : String!
    var team1name : String!
    var team2img : String!
    var team2name : String!
    var time : String!
}

   objectArray = [Sections(sectionDate: "16 june", sectionMatches: [Matches2(team1img: "team1logo", team1name: "teamname", team2img: "team2logo", team2name: "teamname", time: "22:00"),Matches2(team1img: "team1logo", team1name: "team1name", team2img: "team1logo", team2name: "team2name", time: "02:00"),Matches2(team1img: "team1logo", team1name: "team1name", team2img: "team2logo", team2name: "team2name", time: "05:00"),Matches2(team1img: "team1logo", team1name: "team1name", team2img: "team2logo", team2name: "team2name", time: "22:00")]),Sections(sectionDate: "12 may", sectionMatches: [Matches2(team1img: "team1logo", team1name: "team1name", team2img: "team2logo", team2name: "team2name", time: "22:00"),Matches2(team1img: "team1logo", team1name: "team1name", team2img: "team2logo", team2name: "team2name", time: "02:00"),Matches2(team1img: "team1logo", team1name: "team1name", team2img: "team2logo", team2name: "team2name", time: "05:00")]),Sections(sectionDate: "5 august", sectionMatches: [Matches2(team1img: "team1logo", team1name: "team1name", team2img: "team2logo", team2name: "team2name", time: "22:00")])]


  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "matchescell") as! MatchesTableViewCell
        cell.team1img.image = UIImage(named: objectArray[indexPath.section].sectionMatches[indexPath.row].team1img)
        cell.team2img.image = UIImage(named: objectArray[indexPath.section].sectionMatches[indexPath.row].team2img)
        cell.team1name.text = objectArray[indexPath.section].sectionMatches[indexPath.row].team1name
        cell.team2name.text = objectArray[indexPath.section].sectionMatches[indexPath.row].team2name
        cell.time.text = objectArray[indexPath.section].sectionMatches[indexPath.row].time
        cell.selectionStyle = .none
        return cell
   }

这就像我想要的那样工作得很好,但现在我想从中填充这些数据JSON response,我的 json 是这样的:

  "data": [
{
    "date_gregorian": "string",
    "homeName": "string",
    "awayName": "string",
    "time": 1515801600,
},

我得到它是这样的:

   func getMatches(){
    let url = URL(string: APIManager.APIList.baseUrl + APIManager.APIList.urlMatches)!
    var request = URLRequest(url: url)
    request.httpMethod = "GET"
    request.setValue("Basic \(APIManager.APIList.base64Credentials)", forHTTPHeaderField: "Authorization")
    let session = URLSession.shared
    let task = session.dataTask(with: request) {
        (data, response, error) in
        guard error == nil else {
            print("error calling GET")
            print(error!)
            return
        }
        do {
            let matches = try JSONDecoder().decode(Matches.self, from: data!)
            if (matches.data?.isEmpty)!{

                print("no matches available")
            }else{

                for match in matches.data!{

                }
                print(matches.data![0].away)
            }
        } catch  {
            print(error)
            return
        }
    }
    task.resume()
}
}

但我不知道如何添加部分date_gregorian并将迄今为止相同的匹配添加到该部分。

有人可以帮我解决这个问题吗?

标签: iosswiftxcode

解决方案


推荐阅读