在iOS中解析CSV并更新detailview信息

时间:2011-08-11 14:28:34

标签: objective-c ios cocoa-touch csv asihttprequest

我的配置:带有tableView的标签栏 - >点击行 - >的DetailView

我的问题:我用asihttp提出请求。到现在为止还挺好。我收到的答复如下:

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

目前我处理的响应如下:

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

并在:

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

您可以看到我只想在cell.textLabel.textcell.detailTextLabel.text中使用电子邮件和电话

中的名称

有人可以帮我一个例子吗?我浪费了很多时间来解决这个问题,但没找到任何东西


thx for your parser。我现在这样做但没有机会。

tableview的单元格为空 - > cell.textlabel.textcell.detailtextlabel.text

我犯了一个愚蠢的错误吗?

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

1 个答案:

答案 0 :(得分:0)

我编写了一个可以处理此问题的CSV库,即使您的数据不是csv:

https://github.com/davedelong/CHCSVParser

在您的情况下,由于您需要指定自定义分隔符,您可以执行以下操作:

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

当我运行它时,它会记录:

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