2012-04-25 18 views
9

Questo dovrebbe essere facile, ma ho problemi.Come rimuovere la cella dallo statico UITableView creato nello Storyboard

Ho un UITableView statico con una cella che vorrei rimuovere a livello di codice se non è necessario.

Ho un IBOutlet per esso

IBOutlet UITableViewCell * cell15; 

E posso toglierlo chiamando

cell15.hidden = true; 

Questo nasconde, ma lascia uno spazio vuoto in cui la cellula usato per essere e posso' t sbarazzarcene.

Forse un attacco potrebbe essere quello di cambiare l'altezza di esso a 0?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:indexPath 
{ 
//what would I put here? 
} 

Grazie mille!

+1

Che ne dici di 'tableView: deleteRowAtIndexPath:'? Non ho provato da solo, solo un modo veloce per provare – anticyclope

+0

grazie! come sceglierei la riga che voglio cancellare? – dot

+0

Possibile duplicato di [Come rimuovere una cella statica da un UITableView progettato in StoryBoard] (https://stackoverflow.com/questions/8262270/how-to-remove-a-static-cell-from-a-uitableview-designed -in-storyboard) –

risposta

12

Non si può fare molto con questo nell'origine dati dal momento che con le tabelle statiche non hanno nemmeno implementare i metodi datasource. L'altezza è la strada da percorrere.

Prova questa:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (cell == cell15 && cell15ShouldBeHidden) //BOOL saying cell should be hidden 
     return 0.0; 
    else 
     return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 

Aggiornamento

Sembra che, sotto AutoLayout, questo potrebbe non essere la soluzione migliore. C'è una risposta alternativa here che può essere d'aiuto.

+0

Ho ottenuto un 'BAD_ACCESS' in un caso come questo. Il TableView non chiede l'altezza _prima_ la cella viene instanciata? – Besi

+0

Prima chiede (nel qual caso la cella sarà nulla, e passerà al super) e anche durante lo scorrimento, penso, e non vedo come si ottiene un cattivo accesso con questo codice. Dovresti pubblicare una nuova domanda, con un link a questa risposta. – jrturton

+5

Ho anche ottenuto un 'BAD_ACCESS' causato da una sorta di loop infinito. L'ho risolto non confrontando la cella ma il percorso dell'indice in questo modo: 'if (indexPath.row == 3 && cellShouldBeHidden)' – codingFriend1

3

A seconda di come dovrebbe funzionare la tabella, nell'origine dati è possibile implementare tableView:numberOfRowsInSection: per restituire 0 righe per la sezione in base alla logica necessaria.

Aggiornato per il tuo commento:

Il parametro sezione sarà popolata da iOS, quando l'implementazione è chiamato quindi tutto ciò che serve è un interruttore per gestire la sezione che ha la fila si ant rimosso/nascosto. Esempio di seguito:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    switch(section) { 
     case 0: // first section of your table, change for your situation 
      return 0; 
     default: 
      return 0; 
    } 
} 
+0

come selezionare la sezione in codice? questo è qualcosa che sto davvero avendo problemi con ... – dot

+0

Questo lascia uno spazio troppo grande tra le due sezioni adiacenti (non nascoste) però ... – Drux

6

È possibile utilizzare tableView:willDisplayCell e tableView:heightForRowAtIndexPath con l'identificatore di cella per visualizzare/nascondere statiche tableview cellule, ma yo necessario implementare heightForRowAtIndexPath riferendosi a super, non self. Questi due metodi funzionano bene per me:

(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) { 
    [cell setHidden:YES]; 
    } 
} 

e

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
    if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) { 
     return 0; 
} 
    return cell.frame.size.height; 
} 
+0

Funziona perfettamente !! – mikemike396

0

E 'solo il costante di cella

-(void)tableViewSearchPeopleCellHide:(BOOL)hide{ 

    searchCellShouldBeHidden=hide; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]; 
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
    cell.hidden=hide; 
    self.searchPeople.hidden=hide;//UILabel 
    [self.tableView reloadData]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (searchCellShouldBeHidden) //BOOL saying cell should be hidden 
     return 0.0; 
    else 
     return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 
0

La prima cosa che puoi fare è modifica la cellula da storyboard cui si desidera nascondere. Inserisci un numero standard che puoi identificare.

Aggiungere questo codice.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
    if (cell.tag==10) { //I have put 10 for some static cell.  
       cell.hidden=YES; 
       return 0;   

    } 
    cell.hidden = NO; 
    return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 
0

Impostare la cella che si desidera nascondere nascosta da qualche parte nel codice. Aggiungi questo codice: (Se la tua cella ha un'altezza di riga diversa, devi sostituire altre funzioni)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int rowCount=0; 
    for (int row=0; row<[super tableView:tableView numberOfRowsInSection:section]; ++row){ 
     NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:section]; 
     UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path]; 
     if (!cell.hidden){ 
      ++rowCount; 
     } 
    } 
    return rowCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int realRow=-1; 
    for (int row=0; row<[super tableView:tableView numberOfRowsInSection:indexPath.section]; ++row){ 
     NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:indexPath.section]; 
     UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path]; 
     if (!cell.hidden){ 
      ++realRow; 
     } 
     if (realRow==indexPath.row) 
      return cell; 
    } 
    return nil; 
} 
Problemi correlati