views:

52

answers:

2

Hi I have an array of strings separated with a delimeter ":". I tried separating strings using componentsSeparatedByString method and then save the result into an array and later use that array to produce the desired output, but it didn't worked.

For example

Now my UITableView displays cells as:

Case 1:

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

I want the cells to be displayed on UITableView as:

Case 2:

Many How
Many Too
Milk Apple
Simulator iPhone

This is my cellForRowAtIndexPath method:

- (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;
}

The output of the above method is case 1 but I want the output to be case 2 entryKeys has keys from A-Z and entry section has values sorted and listed for respective keys i.e. entries starting with A grouped under key A so on...

It may sound silly but I am still learning..please help me

+2  A: 

There are numerous ways to do this. Here's one;

// 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]];

Hope that helps.

amrox
wow..that worked..the mistake I did was after separating the string, I directly assigned it to the cell textlabel where I get outofbounds exception..thank you..
racharambola
A: 

Wouldn't stringByReplacingOccurrencesOfString:withString: be easier?

rano
That doesn't reverse the split elements, as the OP wanted.
Nicholas M T Elliott
thanks for the reply..the above approach worked..
racharambola
true, my mistake
rano