Objective c 在iOS中解析CSV并更新detailview信息
我的配置:带有tableView的选项卡栏->单击行->详细视图 我的问题是:我用ASIHTT发出请求。到现在为止,一直都还不错。我得到的回答是:Objective c 在iOS中解析CSV并更新detailview信息,objective-c,ios,cocoa-touch,csv,asihttprequest,Objective C,Ios,Cocoa Touch,Csv,Asihttprequest,我的配置:带有tableView的选项卡栏->单击行->详细视图 我的问题是:我用ASIHTT发出请求。到现在为止,一直都还不错。我得到的回答是: name;tel;email Hans Mustermann;0123/45678;info@yourdomain.com Harry the second;98765/12345;my@email.com NSArray *cusNameDataArray = nil; cusNameDataArray = [[response componen
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];
目前,我的反应如下:
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.textlab.text
和cell.detailtextlab.text
电子邮件和电话中输入姓名
谁能帮我举个例子吗?我为这个解决方案浪费了太多时间,但什么也没找到
您的解析器的thx。我现在这样做了,但没有机会
tableview
的单元格为空->cell.textlab.text
和cell.detailtextlab.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];
}
-(void)viewDidLoad
{
[超级视图下载];
self.title=NSLocalizedString(@“客户”,“我的客户”);
NSURL*url=[NSURL URLWithString:@”http://www.yourdomain.com/some.php?do=yes"];
ASIHTTPRequest*request=[AsiHttpRequestWithURL:url];
[请求启动同步];
n错误*错误=[请求错误];
如果(!错误){
NSString*response=[request-responseString];
NSLog(@“%@”,响应);
NSArray*cusDataArray=nil;
cusDataArray=[[response componentsSeparatedByString:@“\n”]retain];
//分配我的客户阵列
self.cusDataArray=[[NSMutableArray alloc]init];
对于(int i=0;i我编写了一个CSV库,可以处理此问题,即使您的数据不是CSV:
在您的情况下,由于需要指定自定义分隔符,您可以执行以下操作:
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"
)
)
从技术上讲,这不是CSV,因为数据不是用逗号分隔的。它更像是SCSV(分号分隔值)StackOverflow是一个面向专业软件开发人员和爱好者的网站。如果您不确定StackOverflow是什么或如何在这里表现,请查看。如果您希望得到好的答案,您必须提出好的问题。像这样的低质量问题在这里不被接受。我强烈建议您阅读以获取提示关于如何提出更好的问题。