0

I was able to sort json response from sample json using alamofire by

self.responseValue.sorted(by: {(dateFormatter.date(from: $0.object(forKey: "date") as! String))?.compare(dateFormatter.date(from: $1.object(forKey: "date") as! String)!) == .orderedDescending})

I am not sure if this is the right way to do this but this sorts the whole response by date. I am trying to display this to a tableview and name section headers by date(from most recent to the last date available in the response). That I was not able to do yet. To be honest I am not sure how to implement that header date. Are you able to provide some suggestions? Thank you.

jiren
  • 7
  • 6
  • Sounds like what you want is something like this https://stackoverflow.com/questions/26596090/adding-sections-separated-by-dates-to-uitableview-in-swift/29755467 but i am not sure. Why don't you parse your response into an object and use an Array of this object as DataSource for your TableView – Teetz Jul 21 '18 at 15:12
  • This is a good example however I am trying to sort an array of NSDictionary with key of "date". If possible I would want to sort it with the "date" key as the header section and the body to the cellforrowat function – jiren Jul 23 '18 at 13:46
  • From my understanding this is exaclty what is shown in the linked SO-Post – Teetz Jul 23 '18 at 13:48
  • how do you implement that in the cellforrowat function? Sorry Im really a bit confused – jiren Jul 24 '18 at 01:20

2 Answers2

0

Yes you can use the below UITableView datasource method to provide section header

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

// Create your view with label here
// access data from your parsed array using the section index
// set the values 
// return the view

}

This is dynamic.

vivekDas
  • 1,248
  • 8
  • 12
0

This solved the problem thanks to Teetz

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! sampleTableViewCell

    cell.sample.text = "sample"
    cell.sample.delegate = self

    return cell
}
jiren
  • 7
  • 6