Sindhia
Sindhia

Reputation: 411

Section title in SectionedTableView

Im creating a Sectioned tableView and I need to set titles programmatically.But the titles are not static.I want to display date and day which are again not static as title for a particular section.I have both of them stored in NSString.

In one context like for one company I have to display like this :

1/12/2013 Saturday                        //section 0
13:00hrs  14:00hrs  15:00hrs

1/13/2013 Sunday                          //section 1
14:00hrs  15:00 hrs

For another company I have to display like this:

1/15/2013 Tuesday                        //section 0
13:00hrs  14:00hrs  15:00hrs

1/16/2013 Wednesday                      //section 1
14:00hrs  15:00 hrs

1/17/2013 Thursday                       //section 2
14:00hrs  15:00 hrs

Like this but here I dont have static number of companies and static dates and days. Im checking for a particular condition and displaying in sections for particular dates and days.And I have to set title as date and day of that section.

How can I do it?

The calculation of date and day goes like this:

-(void)LoadData
{

for(int i=0;i<5;i++)
    {

        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setDay:i];
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate: [NSDate date]options:0];
        [offsetComponents release];
        [gregorian release];


        NSDateFormatter *formatterforGridSectionDate=[[[NSDateFormatter alloc]init] autorelease];
         formatterforGridSectionDate.dateFormat = @"MM/dd/yyyy";
        getDateinGridSection=[[NSString stringWithFormat:@"%@",[formatterforGridSectionDate stringFromDate:nextDate]] componentsSeparatedByString:@""];
        gridSectionDate=[[getDateinGridSection valueForKey:@"description"] componentsJoinedByString:@""];
        NSLog(@"%@",gridSectionDate); // Here I get date in MM/dd/yyyy format 
        weekdayString=[nextDate descriptionWithCalendarFormat:@"%A" timeZone:nil locale:[[NSUserDefaults standardUserDefaults]dictionaryRepresentation]];
        NSLog(@"Day :%@",weekdayString);
        [self check];


    }
      //creating a tableView 


}

-(void)check
{
 //calling the service url for the particular day and date and store the values in array
        NSLog(@"%@",array);

        if([array count]>0)
       {
          [tblViewarray addObject:array];

       }
}

How can I get the date and day as title for which section is loaded in tableview?

Upvotes: 1

Views: 89

Answers (1)

Paras Joshi
Paras Joshi

Reputation: 20541

UPDATE:

Here your requirement is display the date with day name on every section of UITableView.

You have array of Dates for example with name yourDateArray, here i create code for how to display that date on every section with your required date format.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [yourDateArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{


    NSString *str = [yourDateArray objectAtIndex:section];

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];

    NSDate *date = [dateFormatter dateFromString: str];

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"MM/dd/yyyy EEEE"];

    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);

    return convertedString;
}

Upvotes: 2

Related Questions