Quando arriva campo vuoto, rimossa la riga nella visualizzazione raggruppata in Tabella iPhone?

StackOverflow https://stackoverflow.com/questions/4793198

  •  24-10-2019
  •  | 
  •  

Domanda

Ho visualizzato i dati in vista tabella raggruppata. I di dati vengono visualizzati nella vista tabella da parsing XML. Ho 2 sezione della vista tabella, la sezione si ha tre righe e due sezione presenta due righe.

  section 1 ->  3 Rows

  section 2 - > 2 Rows.

Ora voglio controllare, se qualcuno della stringa è vuota, dovrei rimuovere le celle vuote, così ho affrontato alcuni problemi, se ho tolto qualsiasi cella vuota, allora sarà cambiato il numero di indice. Così come posso controllare, nessuno del campo è ?, vuoto Poiché alcune volte più numero di campo vuoto verrà, in modo che la posizione di indice sarà cambiamento. Quindi, vi prego di inviarmi qualsiasi codice di esempio o un link per questo? Come posso ottenere questo?

Codice di esempio,

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section       
 {

 if (section == 0) {

    if([userEmail isEqualToString:@" "] || [phoneNumber isEqualToString:@" "] || [firstName isEqualToString:@" "])
    {
        return 2;

    } 

    else {

        return 3;

    }
}
if (section == 1) {

       if(![gradYear isEqualToString:@" "] || ![graduate isEqualToString:@" "]) {

    return 1;
}
    else
   {
        return 2;
     }


return 0;

}

Si prega di aiutarmi !!!

Grazie.

È stato utile?

Soluzione

Come per la mia comprensione, non volete aggiungere la riga in cui i dati è vuoto, così male suggerisco di perpare i dati sezioni prima di dire la visualizzazione della tabella sulle sezioni e righe.

Quindi, può essere seguente codice può aiutare a ..., ho provato è sufficiente chiamare il metodo "prepareSectionData" dal metodo "viewDidLoad" e definire gli array di sezione nel file h.

- (void) prepareSectionData {
 NSString *userEmail = @"";
 NSString *phoneNumber = @"";
 NSString *firstName = @"";

 NSString *gradYear = @"";
 NSString *graduate = @"";

 sectionOneArray = [[NSMutableArray alloc] init];
 [self isEmpty:userEmail]?:[sectionOneArray addObject:userEmail];
 [self isEmpty:phoneNumber]?:[sectionOneArray addObject:phoneNumber];
 [self isEmpty:firstName]?:[sectionOneArray addObject:firstName];

 sectionTwoArray = [[NSMutableArray alloc] init];
 [self isEmpty:gradYear]?:[sectionTwoArray addObject:gradYear];
 [self isEmpty:graduate]?:[sectionTwoArray addObject:graduate];
}

 -(BOOL) isEmpty :(NSString*)str{
 if(str == nil || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
     return YES;
 return NO;
 }

  // Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0){
    return [sectionOneArray count];
} else if (section == 1) {
    return [sectionTwoArray count];
}
return 0;
}


// Customize the appearance of table view cells.

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



static NSString *CellIdentifier = @"Cell";

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

// Configure the cell.
if(indexPath.section == 0){
    cell.textLabel.text = [sectionOneArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
    cell.textLabel.text = [sectionTwoArray objectAtIndex:indexPath.row];
}
return cell;
}

Altri suggerimenti

@Pugal Devan, Beh, è ??possibile mantenere i dati in un array, ma il problema in questo caso è, si deve prendere cura di limiti di matrice e indici corretti per le diverse sezioni. Per ogni sezione indexPath.row partirà dalla indice 0, e se i dati sono in singolo array, si deve gestire l'indice di riga dal vostro auto. Ma ancora, se si desidera tenerlo, si può fare come:

int sectionOneIndex = 0; 
int sectionTwoIndex = 3;
NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithObjects:@"email", @"Name", @"address", @"zipCode", @"country", nil];

Sopra due interi rappresenta la posizione di partenza di elementi di vostre diverse sezioni. Primi 3 oggetti dalla sezione Array sono la parte della sezione One, e le ultime due oggetti sono la parte della sezione due. Ora è necessario restituire il conteggio delle righe corrette. Per questo si può scrivere:

if(section == 0) return [sectionArray count] - (sectionTwoIndex-1); //returns 3
else if(section == 1) return [sectionArray count] - sectionTwoIndex; //returns 2

o se il conteggio è statica puoi mettere valori costanti in cambio.

E al tempo di leggere dalla matrice, si sarà solo aggiungere questo indice nel valore di riga, che restituirà la posizione corretta del vostro elemento per la cella attiva.

// Customize the appearance of table view cells.

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
if(indexPath.section == 0){
cell.textLabel.text = [sectionArray objectAtIndex:indexPath.row + sectionOneIndex];
} else if (indexPath.section == 1) {
cell.textLabel.text = [sectionArray objectAtIndex:indexPath.row + sectionTwoIndex];
}
return cell;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top