【问题标题】:parsing nested arrays to populate uitableview in swift解析嵌套数组以快速填充 uitableview
【发布时间】: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


【解决方案1】:
  1. 您必须根据需要声明类型的数组实例 FriendDetail 或 MeetingsDetail。

  2. 从 api 获取数据后,解析它并将映射的数据存储到数组实例中。然后,重新加载表格视图。

  3. numberOfRowsInSection 的 tableview 方法使用您的数组计数返回行数。

  4. 您可以使用点表示法访问值,如 "cellForRowAt indexPath:" 方法所示。

    class MeetingVC: UIViewController, UITableViewDelegate, UITableViewDatasource {
     var friendsArr = [FriendDetail]()
    
     func fetchFriendDetail() {
     let decoder = JSONDecoder()
     do {
         let meetingsData = try decoder.decode(TotalMeetings.self, from: response.data!)
         self.friendsArr = meetingsData.friends ?? []//[meetingsData.friends].compactMap({$0}).flatMap({$0})
         print(self.friendsArr) 
         self.tableView.reloadData()
     }catch{
         print(error)
     }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return friendsArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FriendsMeetingTVC
     cell.emailTxt.text = friendsArr[indexPath.row].email
     return cell
    }
    
    }
    

【讨论】:

  • 嘿,这非常有用且有效。请提出一个问题,我如何访问 [MeetingsDetail] 的值
  • 与电子邮件相同:-friendArr[indexPath.row].meetings.
  • 让 meetingId = friendsArr[indexPath.row].meetings[indexPath.row].id 我正在尝试这个 wat,但它显示错误索引超出范围
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 2019-05-15
  • 2015-03-26
  • 2020-02-22
相关资源
最近更新 更多