1

セクション化されたtableViewを作成していますが、プログラムでタイトルを設定する必要があります.しかし、タイトルは静的ではありません.特定のセクションのタイトルとして静的ではない日付と曜日を表示したい.私はそれらの両方をNSStringに保存しています.

ある会社のようなあるコンテキストでは、次のように表示する必要があります。

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

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

別の会社の場合、次のように表示する必要があります。

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

このように、ここでは静的な企業数と静的な日付と曜日はありません。特定の条件をチェックし、特定の日付と曜日のセクションに表示しています。そして、そのセクションの日付と曜日としてタイトルを設定する必要があります。

どうすればいいですか?

日付と曜日の計算は次のようになります。

-(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];

       }
}

テーブルビューにロードされたセクションのタイトルとして日付と曜日を取得するにはどうすればよいですか?

4

1 に答える 1

2

アップデート:

ここでの要件は、 のすべてのセクションに日付と曜日名を表示することですUITableView

たとえば、yourDateArrayという名前の日付の配列があります。ここでは、必要な日付形式ですべてのセクションにその日付を表示する方法のコードを作成します。

- (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;
}
于 2013-01-12T06:40:58.587 に答える