【问题标题】:Section title in SectionedTableViewSectionedTableView 中的部分标题
【发布时间】:2013-01-12 06:30:17
【问题描述】:

我正在创建一个分段的 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];

       }
}

如何获取日期和日期作为在 tableview 中加载哪个部分的标题?

【问题讨论】:

    标签: iphone uitableview xcode4.5


    【解决方案1】:

    更新:

    这里您的要求是在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;
    }
    

    【讨论】:

    • 我能知道什么是时间表吗?
    • @Sindhia 我在这里更新答案,在这里您可以使用数组而不是 NSMutableDictionary 名称为 schedules.. schedules 是 NSMutableDictionary
    • 我想的是每次将日期和日期添加到 NSMutableArray 作为一个对象,并在 titleForHeaderInSection 中使用该数组。但我不明白如何将两个项目(日期和日期)作为一个对象添加到 NSMutableArray .你能告诉我怎么做吗?
    • 你能告诉我如何将两个字符串作为一个对象添加到 NSMutableArray 中的特定索引处吗?我的意思是添加 [array addObject:gridSectionDate,weekdayString];但我遇到了错误。跨度>
    • 嘿,您可以在字典中添加带有键的对象,例如 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:something forKey:@"Some Key"];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多