2013-07-19 39 views
2

我在類別tableview中使用自定義標題。所以,根據這種情況,我必須隱藏標題和細節。所以,我用下面的代碼來顯示帶有條件的標題。在iphone的tableview中隱藏自定義標題

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *aView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]; 


    if(section==0) 
    { 


     if(![appDelegate.purchase_requisitions_preference isEqualToString:@"0"] && ![appDelegate.purchase_requisitions_preference isEqualToString:@"null"]) 
     { 

      UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [btn setFrame:CGRectMake(0, 0, 320, 30)]; 
      btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f]; 
      [btn setTag:section+1]; 
      UILabel *label1 = [[UILabel alloc]init]; 
      label1.frame  = CGRectMake(20, 0, 320, 30); 
      label1.backgroundColor = [UIColor clearColor]; 
      label1.textColor = [UIColor whiteColor]; 
      label1.font = [UIFont boldSystemFontOfSize:16]; 

      label1.text = @"Purchase Order Requisitions"; 
      [label1 setTag:section+1]; 
      [btn addSubview:label1]; 
      [aView addSubview:btn]; 
      [btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown]; 

     } 
     else 
     { 
      return nil; 
     } 


    } 
    else if(section==1) 
    { 

     if(![appDelegate.hr_vacancies_preference isEqualToString:@"0"] && ![appDelegate.hr_vacancies_preference isEqualToString:@"null"]) 
     { 

      UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [btn setFrame:CGRectMake(0, 0, 320, 30)]; 
      btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f]; 
      [btn setTag:section+1]; 
      UILabel *label1 = [[UILabel alloc]init]; 
      label1.frame  = CGRectMake(20, 0, 320, 30); 
      label1.backgroundColor = [UIColor clearColor]; 
      label1.textColor = [UIColor whiteColor]; 
      label1.font = [UIFont boldSystemFontOfSize:16]; 

      label1.text = @"Human Resource Vacancies"; 
      [label1 setTag:section+1]; 
      [btn addSubview:label1]; 
      [aView addSubview:btn]; 
      [btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown]; 
     } 
     else 
     { 
      return nil; 
     } 


    } 
    else if(section==2) 
    { 

     if(![appDelegate.hr_offers_preference isEqualToString:@"0"] && ![appDelegate.hr_offers_preference isEqualToString:@"null"]) 
     { 
      NSLog(@"in offer"); 

      UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [btn setFrame:CGRectMake(0, 0, 320, 30)]; 
      btn.backgroundColor = [UIColor colorWithRed:39.0/255.0 green:131.0/255.0 blue:33.0/255.0 alpha:1.0f]; 
      [btn setTag:section+1]; 
      UILabel *label1 = [[UILabel alloc]init]; 
      label1.frame  = CGRectMake(20, 0, 320, 30); 
      label1.backgroundColor = [UIColor clearColor]; 
      label1.textColor = [UIColor whiteColor]; 
      label1.font = [UIFont boldSystemFontOfSize:16]; 

      label1.text = @"Human Resource Job Offers"; 
      [label1 setTag:section+1]; 
      [btn addSubview:label1]; 
      [aView addSubview:btn]; 
      [btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown]; 

     } 
     else 
     { 

      return nil; 
     } 

    } 
    return aView; 
} 

如果我的情況是錯誤的,那麼我想隱藏我的標題以及它的細節。它的細節越來越隱藏,但標題仍然在那裏顯示。

回答

0

heightForHeaderInSection:方法return 0如果你的數據是零,否則返回30(例如只),如波紋管..

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    if(section==0) 
    { 
     if(![appDelegate.purchase_requisitions_preference isEqualToString:@"0"] && ![appDelegate.purchase_requisitions_preference isEqualToString:@"null"]) 
     { 
      return 30;// any height which you want   
     } 
     else{ 
      return 0; 
     } 
    } 
    // and so on 
} 
0

你需要設置報頭部分的高度,

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    if(section==0 | section==1 |section==2) 
     return 30; 
    else 
     return 0; 
} 
+0

@kels:是這解決了你的問題? – Balu

相關問題