2013-07-20 11 views
11

ho trovato questo tutorial che nasconde una sezione di un TableView statico: http://code-ninja.org/blog/2012/02/29/ios-quick-tip-programmatically-hiding-sections-of-a-uitableview-with-static-cells/Nascondi sezioni di un TableView statico

E le grandi opere, ma solo senza modificarlo, se posso aggiungere una sezione o una riga, funziona male . Sono un principiante e non sono in grado di modificarlo, qualcuno può aiutarmi a nascondere più di una sezione?

Grazie mille!

+0

Qual è lo scopo di cercare di fare una tabella statica sia dinamica? Ecco a cosa servono le tabelle dinamiche. –

+0

Le mie idee erano di implementarlo nella mia app più complicata e lì non posso convertire tutto in dinamico, tuttavia ho (più o meno) risolto! –

risposta

32
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    
    if (section == 2 && _hideTableSection) { 
     //header height for selected section 
     return 0.1; 
    } else { 
     //keeps all other Headers unaltered 
     return [super tableView:tableView heightForHeaderInSection:section]; 
    } 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {   
    if (section == 2 && _hideTableSection) { 
     //header height for selected section 
     return 0.1; 
    } else { 
     // keeps all other footers unaltered 
     return [super tableView:tableView heightForFooterInSection:section]; 
    } 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 1) { //Index number of interested section 
     if (hideTableSection) { 
      return 0; //number of row in section when you click on hide 
     } else { 
      return 2; //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash) 
     } 
    } else { 
     return [super tableView:tableView numberOfRowsInSection:section]; //keeps inalterate all other rows 
    }  
} 
+2

Se la tabella è una tabella di gruppo, è meglio restituire 0.1 per l'altezza di intestazione/piè di pagina. Il ritorno a 0 fa sì che UITableView abbia ancora un gap in cui dovrebbe essere la sezione. – datinc

+0

questa è una buona idea, di solito restituisco 1, ma 0.1 è ancora meglio !!=) –

1

Impostare il valore sezione per 0,01, Qualunque sia la sezione che si voleva nascondere si può provare in questo modo: -

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    CGFloat headerHeight=10.f; 
    if (section==0) 
    { 
     headerHeight=0.01f; 
    } 
    else 
    { 
     headerHeight=50.0f; 
    } 
    return headerHeight; 
} 
2

Se si restituisce 0 per l'altezza della sezione, Apple API ignorerà esso. Quindi, solo restituire un piccolo valore maggiore di 0.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    if (section == 0) { 
    return 1; 
    } 

    return 44; 
} 

implementare anche vista per l'intestazione e tornare a zero per la sezione non si vuole mostrare.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    if (section == 0 && !self.personaCells.count) { 
    return nil; 
    } 

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)]; 
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, headerView.frame.size.width, 20)]; 
    NSString *headerTitle = @"SAMPLE TITLE"; 
    headerLabel.text = headerTitle;  
    [headerView addSubview:headerLabel]; 
    return headerView; 
} 
12

ho voluto condividere un codice che ho scritto per risolvere questo problema dopo aver scavato anche se un sacco di risposte e di sbattere contro molti difetti. Questo è per xCode 7.2.1. (Esempi di codice in Swift)

Il mio caso d'uso era che volevo usare la facilità dello storyboard con raggruppamento statico di TableView, ma avevo bisogno di nascondere particolari sezioni in base ai profili utente. Per farlo funzionare (come descritto in altri post) ho bisogno di nascondere le intestazioni e i piè di pagina, le righe nella sezione E nascondere il testo dell'intestazione/piè di pagina (almeno nella parte superiore). Ho scoperto che se non nascondevo (rendi trasparente) il testo, l'utente poteva scorrere oltre la parte superiore della tabella (sotto il Navigation Controller) e vedere il testo tutto ammassato insieme.

Volevo che fosse facile modificarlo e non volevo che le condizioni si diffondessero attraverso il mio codice, quindi ho creato una singola funzione chiamata shouldHideSection (sezione: Int) che è l'unica funzione che devo modificare per modificare quali righe sono nascosti.

func shouldHideSection(section: Int) -> Bool { 
    switch section { 
    case 0: // Hide this section based on condition below 
     return user!.isProvider() ? false : true 

    case 2: 
     return someLogicForHiddingSectionThree() ? false : true 

    default: 
     return false 
    } 
} 

Ora il resto del codice chiama semplicementeHidesidezione().

// Hide Header(s) 
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForHeaderInSection: section) 
} 

// Hide footer(s) 
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return shouldHideSection(section) ? 0.1 : super.tableView(tableView, heightForFooterInSection: section) 
} 

// Hide rows in hidden sections 
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return shouldHideSection(indexPath.section) ? 0 : super.tableView(tableView, heightForRowAtIndexPath: indexPath) 
} 

// Hide header text by making clear 
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    if shouldHideSection(section) { 
     let headerView = view as! UITableViewHeaderFooterView 
     headerView.textLabel!.textColor = UIColor.clearColor() 
    } 
} 

// Hide footer text by making clear 
override func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 
    if shouldHideSection(section) { 
     let footerView = view as! UITableViewHeaderFooterView 
     footerView.textLabel!.textColor = UIColor.clearColor() 
    } 
} 

ho dovuto sperimentare con molti valori diversi (ritorno 0, 0,1, -1, ...) per ottenere finalmente una soluzione soddisfacente (almeno su iOS 9.x).

Spero che questo sia utile, fammi sapere se hai suggerito miglioramenti.

+1

Ho creato una sottoclasse di UITableViewController chiamata DynamicUITableViewController. Permette di nascondere le sezioni/righe senza dover implementare tutto questo codice nei TableViewControllers. Ecco il repository github: (https://github.com/tkeithblack/DynamicUITableViewController) – KeithB

+1

Ho appena scoperto che anziché restituire -1 quando non si desidera ridurre una riga o un'intestazione se si chiama super.tableView (...) questo quindi onorerà l'altezza riga personalizzata che è impostata nello storyboard. Ho modificato l'esempio sopra e la mia voce github per riflettere questo cambiamento. – KeithB

+0

Nella vista tabella (_: visualizzerà la vista dell'intestazione: per la sezione:) 'e' vista tabella (_: visualizzerà la vista piè di pagina: per la sezione:) 'puoi semplicemente fare' view.isHidden = true' invece di lanciare e rendere 'UILabel' trasparente. – Gary

0

se si cancella il titolo dell'intestazione della sezione dallo storyboard, scompare automaticamente. Con ciò intendo non solo il contenuto del titolo, ma anche lo spazio da esso preso.

2

Per Swift 3

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 

    if section == 2 && hideTableSection { 
     //header height for selected section 
     return 0.1 
    } 

    //keeps all other Headers unaltered 
    return super.tableView(tableView, heightForHeaderInSection: section) 
} 

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 

    if section == 2 && hideTableSection { 
     //header height for selected section     
     return 0.1 
    } 

    return super.tableView(tableView, heightForFooterInSection: section) 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if section == 1 { //Index number of interested section 
     if hideTableSection { 
      return 0 //number of row in section when you click on hide 
     } else { 
      return 2 //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash) 
     } 
    } else { 
     return super.tableView(tableView, numberOfRowsInSection: section) //keeps inalterate all other rows 
    } 
} 

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    if section == 2 && hideTableSection { 
     return "" 
    } 

    return super.tableView(tableView, titleForHeaderInSection: section) 
} 

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { 

    if section == 2 && hideTableSection { 
     return "" 
    } 

    return super.tableView(tableView, titleForFooterInSection: section) 
} 
Problemi correlati