2013-04-30 38 views
0

我想將自定義視圖添加到每個UITableView部分的標題中。我正在使用這個委託方法來返回所需的視圖。它的工作部分是因爲它導致單元部分被分散開來,就好像在那裏有一個標題一樣,然而文本或UIButton實際上都沒有出現。我知道這個方法正在被調用,因爲我在方法中放置了一個NSLog來查看它是否是。我是否犯了一個愚蠢的錯誤,或者這是不是這樣做的正確方法?在UITableView中設置自定義標題視圖

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    { 
     UIView* customView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, tableView.bounds.size.width, 44.0)]; 
     customView.backgroundColor = [UIColor clearColor]; 

     UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44.0)]; 
     headerLabel.textColor = [UIColor darkGrayColor]; 
     headerLabel.font = [UIFont boldSystemFontOfSize:16]; 


     UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     // add button to right corner of section 

     return customView; 
     switch (section) { 
      case 0: 
       headerLabel.text = @"Team Name"; 
       break; 
      case 1: 
       headerLabel.text = @"Captain"; 
       break; 
      case 2: 
       headerLabel.text = @"Wicket Keeper"; 
       break; 
      case 3: 
       headerLabel.text = @"Batting Order"; 
       headerButton.center = CGPointMake(160.0, 22.0); 
       headerButton.backgroundColor = [UIColor blueColor]; 
       headerButton.tag = section; 
       [headerButton addTarget:self action:@selector(enableCellReordering:) forControlEvents:UIControlEventTouchUpInside]; 
       [customView addSubview:headerButton]; 
       break; 
      default: 
       break; 
     } 

     [customView addSubview:headerLabel]; 
     return customView; 
    } 
+0

您是否實現了'tableView:heightForHeaderInSection:'委託方法?如果你實現'tableView:viewForHeaderInSection:',這是必需的。 – rmaddy 2013-04-30 18:08:07

+0

是:'返回44.0' – simonthumper 2013-04-30 18:08:50

+7

爲什麼你返回'customView'兩次? – 2013-04-30 18:10:12

回答

1

returncustomview兩次,switch語句後一個switch語句一個前兩次只需要刪除return customView;switch (section)

2

switch語句之前發現了它,你return customView;

+0

謝謝,你的眼睛比我更銳利哈哈! – simonthumper 2013-04-30 18:11:31

0
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)]; 
    /* Create custom view to display section header... */ 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)]; 
    [label setFont:[UIFont boldSystemFontOfSize:12]]; 
    NSString *string =[list objectAtIndex:section]; 
    /* Section header is in 0th index... */ 
    [label setText:string]; 
    [view addSubview:label]; 
    [view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color... 
    return view; 
} 
+0

歡迎來到SO,@ user3836191。習慣上提供關於代碼功能的解釋,以便其他人更容易理解它。 – 2014-07-24 12:16:40

0

怎麼樣的自定義頁眉origin.x和origin.y? 當我將它們設置爲非零,但它仍然在旁邊邊緣。

相關問題