2013-07-10 15 views
78

Greets, Sto leggendo che il comportamento predefinito di UITableView consiste nel bloccare le righe di intestazione della sezione nella parte superiore della tabella mentre scorri le sezioni finché la sezione successiva non spinge la riga della sezione precedente fuori da vista.UITableView con intestazioni di sezione fissa

Ho un UITableView all'interno di un UIViewController e questo non sembra essere il caso.

E 'solo il comportamento errato per UITableViewController?

Ecco un codice semplificato basato su quello che ho. Mostrerò l'interfaccia UIController e ogni metodo di visualizzazione tabella che ho implementato per creare la vista tabella. Ho una classe di origine dati helper che mi aiuta a indicizzare i miei oggetti da usare con la tabella.

@interface MyUIViewController()<UITableViewDelegate, UITableViewDataSource> 
     @property (nonatomic, readonly) UITableView *myTableView; 
     @property (nonatomic, readonly) MyCustomHelperDataSource *helperDataSource; 
    @end 

    //when section data is set, get details for each section and reload table on success 
    - (void)setSectionData:(NSArray *)sections { 
     super.sectionData = sections; //this array drives the sections 

     //get additional data for section details 
     [[RestKitService sharedClient] getSectionDetailsForSection:someId 
     success:^(RKObjectRequestOperation *operation, RKMappingResult *details) { 
      NSLog(@"Got section details data"); 
      _helperDataSource = [[MyCustomHelperDataSource alloc] initWithSections:sections andDetails:details.array]; 
      [myTableView reloadData]; 
     } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
      NSLog(@"Failed getting section details"); 
     }]; 
    } 

    #pragma mark <UITableViewDataSource, UITableViewDelegate> 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     if (!_helperDataSource) return 0; 
     return [_helperDataSource countSectionsWithDetails]; //number of section that have details rows, ignore any empty sections 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     //get the section object for the current section int 
     SectionObject *section = [_helperDataSource sectionObjectForSection:section]; 
     //return the number of details rows for the section object at this section 
     return [_helperDataSource countOfSectionDetails:section.sectionId]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

     UITableViewCell * cell; 

     NSString *CellIdentifier = @"SectionDetailCell"; 

     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
      cell.textLabel.font = [UIFont systemFontOfSize:12.0f]; 
     } 

     //get the detail object for this section 
     SectionObject *section = [_helperDataSource sectionObjectForSection:indexPath.section]; 

     NSArray* detailsForSection = [_helperDataSource detailsForSection:section.sectionId] ; 
     SectionDetail *sd = (SectionDetail*)[detailsForSection objectAtIndex:indexPath.row]; 

     cell.textLabel.text = sd.displayText; 
     cell.detailTextLabel.text = sd.subText; 
     cell.detailTextLabel.textColor = [UIColor blueTextColor]; 

     return cell; 
    } 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 50.0f; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 30.0f; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section { 
    //get the section object for the current section 
    SectionObject *section = [_helperDataSource sectionObjectForSection:section]; 

    NSString *title = @"%@ (%d)"; 

    return [NSString stringWithFormat:title, section.name, [_helperDataSource countOfSectionDetails:section.sectionId]]; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 0)]; 
    header.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

    header.backgroundColor = [UIColor darkBackgroundColor]; 

    SSLabel *label = [[SSLabel alloc] initWithFrame:CGRectMake(3, 3, 260, 24)]; 
    label.font = [UIFont boldSystemFontOfSize:10.0f]; 
    label.verticalTextAlignment = SSLabelVerticalTextAlignmentMiddle; 
    label.backgroundColor = [UIColor clearColor]; 
    label.text = [self tableView:tableView titleForHeaderInSection:section]; 
    label.textColor = [UIColor whiteColor]; 
    label.shadowColor = [UIColor darkGrayColor]; 
    label.shadowOffset = CGSizeMake(1.0, 1.0); 
    [header addSubview:label]; 

    return header; 
} 
+0

Che dovrebbe funzionare.Mostra un po 'di codice. –

+0

Questo è un modo perfetto per utilizzare UITableView e dovrebbe funzionare con le intestazioni. Cosa non stanno facendo le intestazioni nel tuo caso? – Eric

+0

@Eric le intestazioni stanno scorrendo con le file. Non si fermano in cima al tavolo mentre faccio scorrere. Ho aggiunto il codice che sto usando per generare la mia vista tabella. È un UITableView all'interno di un UIViewController. – topwik

risposta

233

Le intestazioni solo restano fissi quando la proprietà della tabella UITableViewStyle è impostato su UITableViewStylePlain. Se lo hai impostato su , le intestazioni scorreranno verso l'alto con le celle.

+0

il mio stile tableview è UITableViewStylePlain, ma scorre anche con le celle. – yudun1989

+6

Ci sono un paio di possibili spiegazioni per questo. Una nota importante è che le intestazioni rimarranno fisse solo per le celle all'interno della sua sezione. Quindi, se hai un'intestazione nella sezione 0, non rimarrà fissa per le celle nella sezione 1. Un'altra possibile spiegazione è che non stai inizializzando UITableView con lo stile appropriato. Si consiglia di utilizzare 'initWithStyle: UITableViewStylePlain', poiché chiamare qualcosa come tableView.style = UITableViewStylePlain non funzionerà. – bachonk

+0

UITableViewStyleGrouped rende l'intestazione/piè di pagina fissata con altre celle, mentre UITableViewStylePlain rende l'intestazione/il piè di pagina "mobile" quando l'intestazione/piè di pagina raggiunge la parte superiore/inferiore di tableview. – StoneLam

1

È inoltre possibile impostare la proprietà di rimbalzi del TableView su NO. Ciò manterrà le intestazioni di sezione non-floating/statiche, ma poi perderai anche la proprietà bounce di tableview.

+0

Mi hai salvato la vita! Grazie Kevin! – BurtK

0

Cambia la tua TableView Stile:

self.tableview = [[UITableView alloc] initWithFrame: telaio stile: UITableViewStyleGrouped];

Come da documentazione di mela per UITableView:

UITableViewStylePlain- Una vista semplice tavolo. Le intestazioni di sezione o i piè di pagina vengono visualizzati come separatori in linea e fluttuano quando la vista tabella viene scorsa.

UITableViewStyleGrouped: una vista tabella le cui sezioni presentano gruppi distinti di righe . Le intestazioni e i piè di pagina non galleggiano.

Spero che questo piccolo cambiamento vi aiuterà a ..

+1

questo sembra essere esattamente l'opposto di ciò che la domanda ha richiesto - dovrebbe essere 'Plain', non' Grouped' – CupawnTae

1

Swift 3,0

Crea una ViewController con la UITableViewDelegate e UITableViewDataSource protocolli. Quindi crea un TableView al suo interno, dichiarandone lo stile come UITableViewStyle.grouped. Questo risolverà le intestazioni.

lazy var tableView: UITableView = { 
    let view = UITableView(frame: UIScreen.main.bounds, style: UITableViewStyle.grouped) 
    view.delegate = self 
    view.dataSource = self 
    view.separatorStyle = .none 
    return view 
}() 
0

Ora il tuo look Tableview come stile semplice tavolo, ma non galleggiano buz impostazione di stile tavolo insieme al gruppo.

[_tableView setBackgroundView:nil]; 
_tableView.backgroundColor = [UIColor whiteColor]; 
Problemi correlati