开发者

Parse CSV and update detailview information in iOS

My configuration: Tab Bar with tableView -> click on row -> detailview

My problem: I make a request with asihttp. So far so good. I get a response like:

name;tel;email
Hans Mustermann;0123/45678;info@yourdomain.com
Harry the second;98765/12345;my@email.com

At the moment I handle the response like:

NSArray *cusNameDataArray = nil;
cusNameDataArray = [[response componentsSeparatedByString:@"\n"]retain];
self.cusNameDataArray =[[NSMutableArray alloc] initWithArray:cusNameDataArray];
[cusNameDataArray release];

and in:

-(UITableViewCell *)tableView:(UITableView *)cusTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
    UITableViewCell *cell = [self.cusTableView dequeueReusableCellWithIdentifier:identifier];
    if(cell == nil) 
   //cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];


// Set up the cell...
    cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:12];
cell.textLabel.text = **here i want the name**;
cell.detailTextLabel.font = [UIFont fontWithName:@"Verdana" size:10];
cell.detailTextLabel.text = *here i want the email and tel;

return cell;
}

You can see that I just want the name in cell.textLabel.text and in cell.detailTextLabel.text the email and telephone

Can somebody help me a give me an example? I have wasted so much time for this solution but not found anything


thx for your parser. i did it now this way but no chance.

the cells of tableview are empty -> cell.textlabel.text and cell.detailtextlabel.text

did i make a silly mistake?

- (void)viewDidLoad
{
[super viewDidLoad];

self.title = NSLocalizedString(@"Customers", @"My Customers");

NSURL *url = [NSURL URLWithString:@"http://www.yourdomain.com/some.php?do=yes"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
    NSString *response = [request responseString];
    NSLog(@"%@", response);

    NSArray *cusDataArray = nil;
    cusDataArray = [[response componentsSeparatedByString:@"\n"]retain];

    // Allocate my customer array
    self.cusDataArray =[[NSMutableArray alloc] init];

for (int i=0; i<[cusTempArray count]; i++)
{
    NSString *cusLine = [cusTempArray objectAtIndex:i];
    NSArray *cusComponents = [cusLine componentsSeparatedByString:@";"];

    // cusComponents now contains 3 entries - name, number, e-mail. Add this to your customer data array
    [self.cusDataArray addObject:cusComponents];
}

}

[cusDataArray release];
}

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

static NSString *identifier = @"Cell";

UITableViewCell *cell = [self.cusTableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil) 
   //cell = [[开发者_C百科[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];

    // Iterate over ever line and break it up into its components
for (int i=0; i<[cusTempArray count]; i++)
{
    NSString *cusLine = [cusTempArray objectAtIndex:i];
    NSArray *cusComponents = [cusLine componentsSeparatedByString:@";"];

    // cusComponents now contains 3 entries - name, number, e-mail. Add this to your customer data array
    [self.cusDataArray addObject:cusComponents];
}

//Set up the cell...
cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:12];
NSArray *customerData = [self.cusDataArray objectAtIndex:0];
NSString *customerName = [customerData objectAtIndex:0];
NSString *customerPhone = [customerData objectAtIndex:1];
cell.textLabel.text = customerName;
cell.detailTextLabel.font = [UIFont fontWithName:@"Verdana" size:10];
cell.detailTextLabel.text = [self.cusDataArray objectAtIndex:1];

return cell;
}


I wrote a CSV library that can handle this, even though your data isn't csv:

https://github.com/davedelong/CHCSVParser

In your case, since you need to specify a custom delimiter, you'd do something like this:

NSString *data = @"name;tel;email\nHans Mustermann;0123/45678;info@yourdomain.com\nHarry the second;98765/12345;my@email.com";
NSError *error = nil;
NSArray *split = [[NSArray alloc] initWithContentsOfCSVString:data encoding:NSUTF8StringEncoding delimiter:@";" error:&error];
NSLog(@"%@", split);
[split release];

When I run this, it logs:

(
        (
        name,
        tel,
        email
    ),
        (
        "Hans Mustermann",
        "0123/45678",
        "info@yourdomain.com"
    ),
        (
        "Harry the second",
        "98765/12345",
        "my@email.com"
    )
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜