【发布时间】:2020-08-24 16:54:11
【问题描述】:
这是我解析的 API 响应
{
"status": 0,
"message": "Friends found.",
"friends": [
{
"id": 52,
"meetings": [
{
"id": 47,
"meeting_with": "Bbb"
}
]
}
]
}
模型类
struct TotalMeetings: Decodable {
var status: Int
var message: String
var friends: [FriendDetail]?
}
struct FriendDetail: Decodable {
var id: Int
var meetings: [MeetingsDetail]
}
struct MeetingsDetail: Decodable {
var id: Int
var meeting_with: String
}
我这里调用API,调用成功。
var meetingssData :Friends!
let decoder = JSONDecoder()
do{
meetingsData = try decoder.decode(TotalMeetings.self, from: response.data!)
let meet = [self.meetingsData!.friends].compactMap({$0}).flatMap({$0})
print(meetingsData!)
}catch{
print(error)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FriendsMeetingTVC
return cell
}
请指导我如何使用来自 API 调用的响应填充表格视图。
【问题讨论】:
-
对不起,这不是代码编写服务。请阅读此Apple Tutorial 如何创建表格视图。
-
我知道如何创建tableview 我问我如何将数据传递到tableview,将使用哪种方法?
-
甚至不清楚你想显示什么。一个表视图数据源至少需要一个数据源数组。
-
您的
meetingsData必须是实例变量而不是本地变量,以便cellForRowAt可以使用FriendDetail的数组。 (假设你想在你的桌子上显示朋友。)
标签: arrays swift uitableview decodable