2013-12-13 63 views
0

我試圖複製在節首標,在這個圖所示:SectionHeader頭在部分

我嘗試添加標籤同一視圖內的分隔線。 到目前爲止,我有這個代碼不工作在所有...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    CGRect sepFrame = CGRectMake(0, 50-1, 320, 1); 
    UIView *customView = [[UIView alloc] initWithFrame:sepFrame]; 
    customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0]; 

    UILabel *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull]; 
    sectionHeader.backgroundColor = [UIColor whiteColor]; 
    sectionHeader.textAlignment = NSTextAlignmentCenter; 
    sectionHeader.font = [UIFont boldSystemFontOfSize:16]; 
    sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1] ; 
    //sectionHeader.text = [NSString stringWithFormat:@"Section %d", section]; 

    switch (section) { 
     case 0: 
      sectionHeader.text =NSLocalizedString(@"ACTIVO", nil) ; 
      [customView addSubview:sectionHeader]; 
      break; 
     case 1: 
      sectionHeader.text = NSLocalizedString(@"PASSIVO",nil); 
      [customView addSubview:sectionHeader]; 
      break; 
    } 

    return customView; 
} 

任何幫助將提前大加讚賞,因爲我是新來的IOS,謝謝。

+0

爲你的'UILabel'對象提供一些框架 –

+0

請詳細說明:) – Bruno

+1

你的'sectionHeader'沒有任何框架。爲'sectionHeader'設置一些幀值 –

回答

1

下面的代碼應該完成你想要的(包括底部的線):

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0]; 

    UILabel *sectionHeader = [[UILabel alloc] initWithFrame:customView.frame]; 
    sectionHeader.backgroundColor = [UIColor clearColor]; 
    sectionHeader.textAlignment = NSTextAlignmentCenter; 
    sectionHeader.font = [UIFont boldSystemFontOfSize:16]; 
    sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1]; 
    sectionHeader.text = NSLocalizedString(section == 0 ? @"ACTIVO" : @"PASSIVO", nil); 
    [customView addSubview:sectionHeader]; 

    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, customView.frame.size.height - 1, customView.frame.size.width, 1)];  
    lineView.backgroundColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1];  
    [customView addSubview:lineView];  

    return customView; 
} 
1
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    CGRect sepFrame = CGRectMake(0, 50-1, 320, 44); //FIX THIS 
    UIView *customView = [[UIView alloc] initWithFrame:sepFrame]; 
    customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0]; 

    UILabel *sectionHeader = [[UILabel alloc] initWithFrame:sepFrame]; //FIX THIS 
    sectionHeader.backgroundColor = [UIColor whiteColor]; 
    sectionHeader.textAlignment = NSTextAlignmentCenter; 
    sectionHeader.font = [UIFont boldSystemFontOfSize:16]; 
    sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1] ; 
    //sectionHeader.text = [NSString stringWithFormat:@"Section %d", section]; 


    switch (section) { 
     case 0: 
      sectionHeader.text =NSLocalizedString(@"ACTIVO", nil) ; 
      break; 
     case 1: 
      sectionHeader.text = NSLocalizedString(@"PASSIVO",nil); 
      break;     
    } 
    [customView addSubview:sectionHeader]; 

    return customView; 
} 

你需要給你的UI元素適當的框架。在上面的代碼中查找「// FIX THIS」。

1

嘗試使用此方法

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    CGRect sepFrame = CGRectMake(0, 50-1, 320, 50); // change here your view's height 
UIView *customView = [[UIView alloc] initWithFrame:sepFrame]; 
customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0]; 

UILabel *sectionHeader = [[UILabel alloc] initWithFrame:sepFrame]; // also change here in label frame 
sectionHeader.backgroundColor = [UIColor whiteColor]; 
sectionHeader.textAlignment = NSTextAlignmentCenter; 
sectionHeader.font = [UIFont boldSystemFontOfSize:16]; 
sectionHeader.textColor = [UIColor colorWithRed:(66/255.0) green:(139/255.0) blue:(18/255.0) alpha:1] ; 
//sectionHeader.text = [NSString stringWithFormat:@"Section %d", section]; 

switch (section) { 
    case 0: 
     sectionHeader.text =NSLocalizedString(@"ACTIVO", nil) ; 
     [customView addSubview:sectionHeader]; 
     break; 
    case 1: 
     sectionHeader.text = NSLocalizedString(@"PASSIVO",nil); 
     [customView addSubview:sectionHeader]; 
     break; 
} 

return customView; 

} 

//還寫這個方法

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 50; 
} 
+0

謝謝,但我只是想複製設計沒有別的:) – Bruno

0

查看以下文檔:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

它指出,還必須實現:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; 

它才能正常工作。