Create a table for UITableView headers


In my iPhone app I want to show a table view with section headers. I have a datasource like this(Example)

     {date : 12-10-2014  rowdata:abc},
     {date : 12-10-2014  rowdata:pqr},
     {date : 12-10-2014  rowdata:xyz},
     {date : 13-10-2014  rowdata:123},
     {date : 13-10-2014  rowdata:780},
     {date : 14-10-2014  rowdata:tuv},

I want to show the section header as the date - and have to show row data in it like this (just consider it as a format of table view-dates are it header views and below rows)


please give me a logic to do how to change or create new arrays with my data source, I am clear working with sectioned tableview and header views.

i can create a seperate array with dates for header view, but how can I show rowdata under every section, because in every section row starts with index 0.

You are basically asking how to categorise your rowdata data by its date key.

Simple. You need to do the following:

  1. Create data models for your data couplets so that its nicer to populate your table with and store in arrays :)
  2. Create a dictionary that will store the date as keys - you can imagine them as categories - and the row data as objects stored in an array for each category.

Step 1: Create your custom data model to make data management more easier and cleaner. Create two new files called rowDataModel.h and rowDataModel.m. They will be a subclass of the NSObject class.

This is what your .h file would look like:

#import <Foundation/Foundation.h>

@interface rowDataModel : NSObject
@property (nonatomic, retain) NSString * rowDataDate;
@property (nonatomic, retain) NSString * rowDataInformation;


and in your .m file you will have this:

#import "rowDataModel.h"

@implementation rowDataModel
@synthesize rowDataDate;
@synthesize rowDataInformation;


    if (self = [super init]) {
        self.rowDataDate = [data objectForKey:@"date"];
        self.rowDataInformation = [data objectForKey:@"rowdata"];

    return self;

We are doing this as this will contain easy to work with containers. Its a good way to manage your custom json objects too.

Step 2 Now in your view controller where you will be populating the database and categorising your data, make sure you import your custom rowDataModel.h file

NSMutableDictionary *myData = [[NSMutableDictionary alloc] init];

//Have a for loop that iterates through your array
for(NSDictionary *currentItem in serverArray){
    RowData *newItem = [[RowData alloc] initWithJSONData:currentItem];

    //If category the date key does not exist, then create a new category for it
    if([myData objectForKey:newItem.rowDataDate] == nil){
        //We want to create an array for each category that will hold all the row datas
        [myDate setObject:[NSMutableArray new] forKey:newItem.rowDataDate];
    //Grab the array from a particular category and add the current item to this category
    [[myDate objectForKey:newItem.rowDataDate] addObject:newItem];

Now when you are populating your table delegate methods, in your titleForHeader tableview delegate methods, you simply grab the category titles: the dates from your myData dictionary using the allKeys property, and call the `objectForIndex: method for the section counter in your delegate method, ad you use that string to set the title of your header view by using the titleForHeader table view delegate method.

And in your cellForRowAtIndexPath: delegate method, you simply grab the correct category in your myData array by using the keys, and then access the individual objects in the array based on the row count.

And thats all bro. Goodluck

disclaimer, I have not tested this code, this should work right off the bat :)