ios - 从 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
并将迄今为止相同的匹配添加到该部分。
有人可以帮我解决这个问题吗?
解决方案
推荐阅读
- node.js - 使用 TypeScript 在 Node.js 中进行 Sequelize - 没有“new”就无法调用类构造函数模型
- scala - 是否可以在基于 Scala 的项目中使用 MLFlow 和 H2o.ai 苏打水?
- java - 通过 POST 从 Angular 向 Springboot 发送数据
- python - 如何用空格和标点符号分割字符串,但保留标点符号?
- php - 如何确定电子邮件地址是否以某些内容开头并以域结尾
- javascript - 如何在页面加载时运行功能,然后在点击后运行?
- python - 找到所有可能的在结束和开始重叠的组合
- r - R:ggplot2 密度图显示错误的填充颜色
- c# - 执行表连接后在 C# 中处理 lambda 查询结果
- prolog - 非常基础:如何在 Prolog 中创建和管理列表?