I currently have a json response from json which is a [NSDictionary]. This is displayed in a tableview and I was able to set the sorted dates into headers however I am having a difficulties in setting the uilabel in the cellForRowAt function. The tableview I was looking to display has a header title of sorted dates(which I already have) and under the section are the names with the same date as the header. I have provided the code for this below. Thank you for your help and suggestions.
import UIKit
import Alamofire
class JSONTableViewController: UITableViewController
{
var responseValue:[NSDictionary] = []
var sortedResponsevalue:[NSDictionary] = []
var sectionHeaderArray:[String] = []
var rowTitle:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for response in self.responseValue {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let responseDate = response.object(forKey: "date") as! String
let date = dateFormatter.date(from: responseDate)
print(date!.toString(dateFormat: "MMM d, yyyy"))
self.sectionHeaderArray.append(date!.toString(dateFormat: "MMM d, yyyy"))
self.rowTitle.append(response.object(forKey: "name") as! String)
}
print(self.sectionHeaderArray.count)
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sectionHeaderArray[section]
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return self.sectionHeaderArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.sectionHeaderArray[section].count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "jsonCell", for: indexPath) as! JSONTableViewCell
if let nameString = self.responseValue[indexPath.row].object(forKey: "name") as? String {
cell.jsonLabel.text = nameString
}
return cell
}
}