2010-08-20 2 views
0

Salut J'ai un tableau de chaînes séparées par un délimiteur ":". J'ai essayé de séparer les chaînes à l'aide de la méthode componentsSeparatedByString, puis d'enregistrer le résultat dans un tableau, puis d'utiliser ce tableau pour produire la sortie souhaitée, mais cela n'a pas fonctionné.Chaîne de fente et d'échange en Objective C (iPhone)

Par exemple

Maintenant, mon UITableView affiche les cellules comme:

Cas 1:

How:Many 
Too:Many 
Apple:Milk 
iPhone:Simulator 

Je veux que les cellules à afficher sur UITableView comme:

Case 2:

Many How 
Many Too 
Milk Apple 
Simulator iPhone 

C'est ma méthode de cellForRowAtIndexPath:

- (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]; 
    } 
NSString *entry=[entryKeys objectAtIndex:[indexPath section]]; 
    NSArray *entrySection=[entries objectForKey:entry]; 
    cell.textLabel.text=[entrySection objectAtIndex:[indexPath row]] 
} 

return cell; 
} 

La sortie de la méthode ci-dessus est cas 1 mais je veux que la sortie soit cas 2 entryKeys a des clés de A à Z et de la section d'entrée a des valeurs triées et listées pour les clés respectives ie les entrées commençant par A groupées sous la clé A ainsi de suite ...

Cela peut sembler idiot mais j'apprends encore ... s'il vous plaît aidez moi

Répondre

3

Il existe de nombreuses façons de procéder. En voici un.

// get your raw text in the form of "AAA:BBB" 
NSString *rawText = [entrySection objectAtIndex:[indexPath row]]; 

// split the text by the : to get an array containing { "AAA", "BBB" } 
NSArray *splitText = [rawText componentsSeparatedByString:@":"]; 

// form a new string of the form "BBB AAA" by using the individual entries in the array 
NSString *cellText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]]; 

Espérons que ça aide.

+0

wow..qui a fonctionné .. l'erreur que j'ai faite était après avoir séparé la chaîne, je l'ai directement assigné à la cellule textlabel où je reçois l'exception outofbounds..thank .. – racharambola

0

Le code stringByReplacingOccurrencesOfString:withString: ne serait-il pas plus facile?

+0

Cela n'inverse pas les éléments de split, comme l'OP voulait. –

+0

merci pour la réponse..la méthode ci-dessus a travaillé .. – racharambola

+0

vrai, mon erreur – rano

Questions connexes