从JSON响应填充tableview部分和行

时间:2018-08-02 14:20:16

标签: ios swift xcode

我有一个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添加部分,并向该部分添加迄今为止具有相同内容的匹配项。

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

0 个答案:

没有答案