【问题标题】:UItableView: Nested Header sectionsUItableView:嵌套的标题部分
【发布时间】:2016-01-19 07:29:18
【问题描述】:

在这里,我设置了描述、价格以及从到到的位置,但无法设置如上图所示的日期和时间。格式面临一些问题,以在一个标题中设置日期,在另一个标题中设置时间。如果任何显示如何设置嵌套标题的链接对我有很大帮助。

提前致谢。

【问题讨论】:

  • 您是否正在努力在两行中添加带有日期和时间的标题,中间有一些分隔符,如图所示?
  • 是的。如图m不能在中间添加分隔符。

标签: ios objective-c nsdate nsdateformatter


【解决方案1】:

是的,如图不能在中间加分隔符。我也试过这个

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
NSMutableArray *arrdate=[[NSMutableArray alloc]init];
arrdate=[date1 objectAtIndex:section] ;
NSLog(@"%@",arrdate);
NSMutableArray *arrtime=[[NSMutableArray alloc]init];
arrtime=[time objectAtIndex:section];
NSString *strvalue=[arrdate objectAtIndex:0];
UIView *view;

if([strvalue isEqualToString: strcheck])
{
    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 15)];
    /* Create custom view to display section header... */
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 15)];
    [label1 setFont:[UIFont boldSystemFontOfSize:12]];
    strcheck=[arrdate objectAtIndex:0] ;
    NSString *string =[arrtime objectAtIndex:0];
    /* Section header is in 0th index... */
    [label1 setText:string];
    [view addSubview:label1];

}
else{
    /* Create custom view to display section header... */
    view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
    //main time
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 15)];
    [label setFont:[UIFont boldSystemFontOfSize:12]];
    strcheck=[arrdate objectAtIndex:0] ;
    /* Section header is in 0th index... */
    [label setText:strcheck];
    [view addSubview:label];


    //time..
    UILabel *labeltime = [[UILabel alloc] initWithFrame:CGRectMake(10,20, tableView.frame.size.width, 15)];
    [labeltime setFont:[UIFont boldSystemFontOfSize:12]];

    NSString *stringtime =[arrtime objectAtIndex:0];
    /* Section header is in 0th index... */
    [labeltime setText:stringtime];
    [view addSubview:labeltime];
    }
return view;

}

但在滚动表格视图时面临日期视图受到干扰的问题。

【讨论】:

    【解决方案2】:

    创建一个 xib 文件,其中包含 UIView 和其中的标签,如随附的屏幕截图所示。

    1. 有一个名为CustomHeader 的源文件,它是UIView 的子类。
    2. 使用CustomHeader 类映射您的xib 视图。
    3. 为xib中添加的标签生成IBOutlets。

    在您的ViewController viewForHeaderInSection 方法中,

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSArray * bundleArray = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil];
    CustomUIClass * view = (CustomUIClass *) [bundleArray objectAtIndex:0];
    return view;
    

    }

    同时覆盖heightForHeaderInSection

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 69.0f;
    

    }

    这里的 '69' 是 xib 的高度。现在您的 tableview 看起来像附加的屏幕截图。

    像这样,您可以根据需要自定义标题。

    【讨论】:

      猜你喜欢
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 2012-04-05
      • 1970-01-01
      相关资源
      最近更新 更多