2013-04-30 3 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:' delegate? Это необходимо, если вы реализуете 'tableView: viewForHeaderInSection:'. – rmaddy

+0

yes: 'return 44.0' – simonthumper

+7

Почему вы возвращаете' customView' дважды? –

ответ

1

return Вы ваш customview дважды, один перед включением заявления одного после включения заявления дважды просто нужно удалить return customView; перед тем switch (section)

2

Нашел, вы return customView; до switch заявления.

+0

Спасибо, у тебя острые глаза, чем у меня, ха-ха! – simonthumper

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. Обычно можно дать объяснение функциональности вашего кода, чтобы облегчить понимание другими людьми. –

0

Как насчет origin.x пользовательского заголовка и origin.y? , когда я установил их отличные от нуля, но это все еще рядом с реброми.