2013-03-01 20 views
28

Voglio rendere tableView con più sezioni ma non voglio usare il dizionario ho due array voglio che il primo array debba essere caricato nella prima sezione e il secondo nella seconda sezione.UITableView con più sezioni

Ho arrayOne con 3 elementi e arrayTwo con 4 elementi, quindi come aggiungerli e mostrarli in sezioni.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section == 0) 
     return resultArray.count; 
    else 
     return resultArray.count; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSLog(@"Number of Sections"); 
    if(section == 0) 
     return @"Section 1"; 
    if(section == 1) 
     return @"Section 2"; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"Table Cell Data"); 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (indexPath.section==0) {  
     appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate]; 
     ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     NSLog(@"Cell Values %@",cellValue); 
     cell.textLabel.text = cellValue; 
     return cell; 
    } 
    else { 
     ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     cell.textLabel.text = cellValue; 
     return cell;  
    } 
} 
+1

Qual è il problema che si sta affrontando? Il tuo codice sembra corretto. – Rushi

+0

il valore di assegnazione nella cella fornisce la sezione di errore della sezione non dichiarata –

+0

È necessario utilizzare indexPath.section e non solo la sezione in cellForRowAtIndexPath. Il resto del tuo codice va bene. – Rushi

risposta

80
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return 2 ; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     if (section==0) 
     { 
      return [array1 count]; 
     } 
     else{ 
      return [array2 count]; 
     } 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
     if(section == 0) 
      return @"Section 1"; 
     else 
      return @"Section 2"; 
} 


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

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     } 

if (indexPath.section==0) { 
    ObjectData *theCellData = [array1 objectAtIndex:indexPath.row]; 
    NSString *cellValue =theCellData.category; 
    cell.textLabel.text = cellValue; 
} 
else { 
    ObjectData *theCellData = [array2 objectAtIndex:indexPath.row]; 
    NSString *cellValue =theCellData.category; 
    cell.textLabel.text = cellValue; 
} 
    return cell;   
} 
+0

Questo - (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section { if (indexPath.section == 0) { return [numero di array1]; } else { return [numero di array1]; } } è sbagliato. Non è possibile ottenere indexPath.section qui. – Rushi

+0

Sì sì giusto modificato grazie Rushi –

+0

non mostra valori nei valori di cella –

0

Modificare questa if (section==0)

-if (indexPath.section==0)

Bisogna implementare questa funzione:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section == 0) 
     return arrSection1.count; 
     else 
     return arrSection2.count; 
} 
+0

di nuovo si sta dando errore ho fatto come questo indexPath.section –

+0

@NaziaJan Controlla il codice modificato – Rushi

+0

@NaziaJan hai implementato questa funzione? – Rushi

0

tuo numberOfRowsInSection dovrebbe essere qualcosa di simile:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) { 
     return firstArray.count; 
    } else { 
     return secondArray.count; 
    } 
} 
0

Siete sulla buona strada quasi giusto ....

Consideriamo i tuoi due array sono arrOne e arrTwo .....

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     if(section == 0) 
     return [arrOne count]; 
     else 
     return [arrTwo count]; 
    } 

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

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
      } 

      if (indexPath.section==0) { 


     ObjectData *theCellData = [arrOne objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     NSLog(@"cellValue = %@",cellValue); //To see the value of string cellValue 
     cell.textLabel.text = cellValue; 

     return cell; 

    } 

    else { 
     ObjectData *theCellData = [arrTwo objectAtIndex:indexPath.row]; 
     NSString *cellValue =theCellData.category; 
     cell.textLabel.text = cellValue; 

     return cell;   
    } 
} 
+0

sta dando errore a indexpath.section –

+0

suppongo che P dovrebbe essere maiuscolo in indexpath, .... averlo modificato. Ora prova, dovrebbe funzionare. –

+0

funziona ma non mostra valori cella –

0

TableView delegato Metodo già dare la sezione come : (NSInteger) sezione .. quindi possiamo anche usare switchCase

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     switch (section) 
     { 
      case 0: 
       return self.arrMedia.count; 
       break; 
      case 1: 
       return 1; 
       break; 
     } 
     return 0; 
    } 
0

Qui ci sono le pertinenti Swift 3 linee:

func numberOfSections (in tableView: UITableView) -> Int { 
    ... 
    return numberOfSections 
} 

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    //Not necessary, but will let you format the section headers. Example: 
    guard let header = view as? UITableViewHeaderFooterView else { return } 
    header.textLabel?.font = UIFont.boldSystemFont(ofSize: 24) 
    header.textLabel?.textColor = UIColor.darkGray 
    header.textLabel?.textAlignment = .center 
    header.textLabel?.frame = header.frame 
    view.tintColor = UIColor.white 
} 

func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { 
    //Also not necessary, but clear footers help space out sections 
    view.tintColor = UIColor.clear 
} 

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    //Spacing between sections: 
    return 25 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    ... 
    return sectionCount 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    //All your cell setup 
    ... 
    //I call this to get the absolute row number (disregarding sections) 
    let row = getAbsoluteRow_InCurrentSection(indexPath: indexPath) 
    //Now you can use the row number to grab a value from an array or CoreData object and use the value to populate your cell!! 
} 

func getAbsoluteRow_InCurrentSection(indexPath: IndexPath) -> Int { 
    var aggRows = 0 
    if currentListEntity == "GrocTask" { 
     let curSection = indexPath.section 
     for i in 0..<curSection { 
      aggRows += flex1ArrayCnt[i] 
     } 
     aggRows += indexPath.row 
    } 
    return aggRows 
} 

// Si prega di essere a conoscenza, ho incluso solo quelle direttamente rilevanti per la costruzione di un UITableView con sezioni. Non ho incluso editActions, didSelect o altre funzioni delegate di UITableView.

Problemi correlati