1

I want to show section title as January,February,March(as per data comes from api) instead of showing January, March, February.

I am getting this data from backend api.

wholeDic : NSDictionary = {

January =     (
            {
        date = "20-jan-18";
        subtitle = "Only four days remaining";
        title = "School fees of August, 2018School fees of August, 2018";
    },
            {
        date = "21-jan-18";
        subtitle = Holi;
        title = "Holiday on third march";
    }
);
february =     (
            {
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    },
            {
        date = "20-12-18";
        subtitle = "Holiday issue by Govt. of India";
        title = "Bharat Band";
    }
);
march =     (
            {
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    },
            {
        date = "20-feb-18";
        subtitle = "Paricipate in this activity";
        title = "Annual function will be held in feb,2018";
    }
);}

Now I have fetched "key" and added in array

for (key, value) in wholeDic{
   sectionTitleArray.add(key)
   print(sectionTitleArray)
}

and when I print sectionTitleArray than console is showing January, March, February instead of showing January, February, March.

I know that Dictionary is an unordered collection but I want to know that how to fetch keys by order?

My UitableView DataSource & Delegate are

func numberOfSections(in tableView: UITableView) -> Int{

    return sectionTitleArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    let sectionTitle : String = sectionTitleArray.object(at: section) as! String
    let sectiondes:NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
    return sectiondes.count

}

This is my tableView .Everything is working fine but I want to display month like JANUARY,FEBRUARY,MARCH as per api data.

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! frameNboundTableViewCell
    let sectionTitle : String = sectionTitleArray.object(at: indexPath.section) as! String
    let contentArr : NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray
    let contentDic : NSDictionary = contentArr.object(at: indexPath.row) as! NSDictionary
    print(contentDic)
    cell.titleLbl.text = contentDic.value(forKey: "title") as? String
    cell.titleLbl.numberOfLines = 0
    return cell

}
Mahmut Acar
  • 713
  • 2
  • 7
  • 26
  • problem in api response. you not get same time same sequence of key. instead you use January: dictionary use array. – Sagar Bhut Sep 11 '18 at 13:46
  • So what if the api response is January, March, April, will you be showing February? – George Sep 11 '18 at 13:49
  • @George:-No need to show February.I want to fetch all the key as a order wise like January,february and march.But when i adding key in array than it showing January,march,february.I have to show all the key as a header in uitableview (order wise name as per api data sequence).Please have a look on my image shared. – Gaurav singh Sep 12 '18 at 07:39
  • @SagarBhut : - I think there is no any problem in api response.I am getting the same data every time.Please have a look on my image shared and please do some suggestion. – Gaurav singh Sep 12 '18 at 07:44
  • if the api response is like February, January, April, March how do you want to show in the UI? will it be February, January, April, March or will it be January, February, March, April? – George Sep 12 '18 at 08:22
  • @George : It will be like February, January, April, March.Thanks for replying. – Gaurav singh Sep 12 '18 at 09:40
  • @George : Either you can suggest me how will be like data structure if the number of section in UiTableView is dynamic ?? so that I can edit my api response. – Gaurav singh Sep 13 '18 at 05:03
  • @Gauravsingh sorry for the delay, if you want to edit your api response the best way would be returning the data as an array that way you can simply retrieve using index. – George Sep 13 '18 at 05:37
  • @George : Got the solution. Problem is in api response.Data structure will b like: ({ Item = ({ date = "20-jan-18"; subtitle = "Only four days remaining"; title = "School fees of August, 2018School fees of August, 2018"; },{ date = "21-jan-18"; subtitle = Holi; title = "Holiday on third march";},{ date = "21-April-18"; subtitle = "New Session"; title = "School Reopen on 5th, April 2018";}); Title = January; },)Thanks – Gaurav singh Sep 13 '18 at 07:21
  • I have added the answer. Please check – George Sep 13 '18 at 07:22

2 Answers2

0

You should make the first letter capital:

extension String {
    func capitalizingFirstLetter() -> String {
        return prefix(1).capitalized + dropFirst()
    }

    mutating func capitalizeFirstLetter() {
        self = self.capitalizingFirstLetter()
    }
}

Then section title:

let sectionTitleStr = sectionTitleArray.object(at: indexPath.section) as! String
let sectionTitle : String = sectionTitleStr.capitalizingFirstLetter()
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26
  • I think no need to convert the title in CAPITAL letter because when I am searching array of that key..it will crash because in api response there is no any key of CAPITAL letter.See the below code: let contentArr : NSArray = wholeDic.object(forKey: sectionTitle) as! NSArray .It will crash. – Gaurav singh Sep 13 '18 at 04:57
0

Based on the comments you can change the api response into array and simply access with index. The json response will look like this

    {
  "data" : [
    {
      "month" : "January",
      "details" : [
        {
          "date" : "20-jan-18",
          "subtitle" : "Only four days remaining",
          "title" : "School fees of August, 2018School fees of August, 2018"
        },
        {
          "date" : "21-jan-18",
          "subtitle" : "Holi",
          "title" : "Holiday on third march"
        }
      ]
    },
    {
      "month" : "february",
      "details" : [
        {
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        },
        {
          "date" : "20-12-18",
          "subtitle" : "Holiday issue by Govt. of India",
          "title" : "Bharat Band"
        }
      ]
    },
    {
      "month" : "march",
      "details" : [
        {
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        },
        {
          "date" : "20-feb-18",
          "subtitle" : "Paricipate in this activity",
          "title" : "Annual function will be held in feb,2018"
        }
      ]
    }
  ]
}

Now you can access the first object from the json array and then retrieve the respective key and display it. It will be something like

let object = response.data![index]
object.month // "January"

Hope it helps

George
  • 3,600
  • 2
  • 26
  • 36
  • Yes, it is very useful for me.This is what I am looking for.Thanku so much for your valuable time given here.Sorry I can't upvote you because my reputation is too low.This is the actual data pattern for making section dynamic in uitableview.Hope it will help so many guys here. – Gaurav singh Sep 13 '18 at 08:16