如何用字典中的分组结构中的数据填充UITableView?

问题描述 投票:0回答:2

我有以下结构包含来自解析的JSON的数据。

struct callRailData {
var callDate: String
var userDetails = callData()
}

struct callData{
var callerName: String?
var callerNumber: String?
var callerCityName: String?
var callerCountryName: String?
var callerStateName: String?
var callAnsweredState: Bool?
var callDirection: String?
var callDuration: String?
var callRecordingURL: String?
var callRecordingPlayer: String?
var callSource: String?
var callCompanyName: String?
}

我需要在UITableView中显示这些数据,并将'callDate'作为节头。所以我在这个字典中对结构进行了分组:

var user = [callRailData]()

var sections = Dictionary<String, [callRailData]>()
sections = Dictionary(grouping: user, by: { $0.callDate }). 

我不知道如何在tableview中显示这些数据。如何从'sections'获取numberOfSections和numberOfRowsInSection。请帮助我成为Swift和iOS开发的初学者。

ios swift uitableview dictionary struct
2个回答
0
投票

通常,我们使用数组作为tableview的数据源。从数组中提供numberOfRows =数组计数和“cellForRow”数据源对象。


0
投票

我认为你不需要像那样进行分组......就像那样希望它会起作用。

  override func numberOfSections(in tableView: UITableView) -> Int {
      return user.count
    }

它将根据您的用户数量制作部分。现在每个部分都有一行显示数据。

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return 1
        }



 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "your cell", for: indexPath) as! Your Cell class
         cell.callerNameLabel.text = user[indexpath.section].userdetails.callerName 
        return cell
    }

此func将设置您的第二个标题标题

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
       return user[section].callDate
    }
© www.soinside.com 2019 - 2024. All rights reserved.