2012-04-19 13 views

risposta

31

È necessario creare il proprio punto di vista intestazione:

implementare all'interno della vostra origine dati Tableview/delegare

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    if (sectionTitle == nil) { 
     return nil; 
    } 

    // Create label with section title 
    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.frame = CGRectMake(20, 6, 300, 30); 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green 
           saturation:1.0 
           brightness:0.60 
             alpha:1.0]; 
    label.shadowColor = [UIColor whiteColor]; 
    label.shadowOffset = CGSizeMake(0.0, 1.0); 
    label.font = [UIFont boldSystemFontOfSize:16]; 
    label.text = sectionTitle; 

    // Create header view and add label as a subview 

    // you could also just return the label (instead of making a new view and adding the label as subview. With the view you have more flexibility to make a background color or different paddings 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)]; 
    [view autorelease]; 
    [view addSubview:label]; 

    return view; 
} 
+0

Si può solo restituire l'etichetta, non v'è alcun vantaggio in aggiunta come una visualizzazione secondaria ad una vista altrimenti vuoto. – jrturton

+0

Sì, sì .... ma forse ti piacerebbe riposizionare la tua etichetta o qualcosa di simile ... –

+0

grazie a tonnellate !!!. Quando l'etichetta viene restituita direttamente, l'etichetta NON è posizionata nella posizione X 6. Basandomi, a mio avviso, che viene sempre restituita la vista, è 'frame.origin.x' e' frame.origin.y' viene ignorata. Quindi, quando l'etichetta viene aggiunta come vista secondaria alla vista e viene restituita la vista, l'etichetta 'frame.origin.x' e' frame.origin.y' viene preservata bcuz è relativa alla sua super vista. – user1046037

14

Può fare anche questo:

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{ 
    [[((UITableViewHeaderFooterView*) view) textLabel] setTextColor:[UIColor whiteColor]]; 
} 

.....

+1

+ 1 questo è esattamente quello che stavo cercando, grazie – anneblue

+0

Non funzionerà su iOS 5. – Dmitry

+0

Funziona perfettamente grazie – jose920405

1

Sono riuscito a visualizzare l'intestazione solo a opo aggiungendo altezza intestazione insieme con la vista

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    return 110; 
} 
Problemi correlati