1

我已经寻找了很长时间的答案,但仍然没有找到答案,所以我想我会自己问这个问题。

在我的 iPad 应用程序中,我需要能够解析 .csv 文件以填充表格。我正在使用http://michael.stapelberg.de/cCSVParse来解析 csv 文件。但是,我只成功解析了本地文件。我一直在尝试从服务器访问文件,但无处可去。

这是我解析本地 .csv 文件的代码:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        //UITextField *reply = [alertView textFieldAtIndex:buttonIndex];
        NSString *fileName = input.text;
        NSLog(@"fileName %@", fileName);
        CSVParser *parser = [CSVParser new];
        if ([fileName length] != 0)
        {
            NSString *pathAsString = [[NSBundle mainBundle]pathForResource:fileName ofType:@"csv"];
            NSLog(@"%@", pathAsString);

            if (pathAsString != nil)
            {
                [parser openFile:pathAsString];
                NSMutableArray *csvContent = [parser parseFile];
                NSLog(@"%@", csvContent);
                [parser closeFile];
                NSMutableArray *heading = [csvContent objectAtIndex:0];
                [csvContent removeObjectAtIndex:0];

                NSLog(@"%@", heading);

                AppDelegate *ap = [AppDelegate sharedAppDelegate];
                NSManagedObjectContext *context = [ap managedObjectContext];

                NSString *currentHeader = [heading objectAtIndex:0];
                NSString *currentValueInfo = [heading objectAtIndex:1];
                NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Field" inManagedObjectContext:context];
                [newObject setValue:@"MIS" forKey:@"header"];
                [newObject setValue:currentHeader forKey:@"fieldName"];

                for (NSArray *current in csvContent)
                {
                    NSManagedObject *newField = [NSEntityDescription insertNewObjectForEntityForName:@"Field" inManagedObjectContext:context];
                    [newField setValue:currentHeader forKey:@"header"];
                    [newField setValue:currentValueInfo forKey:@"valueInfo"];
                    NSLog(@"%@", [current objectAtIndex:0]);
                    [newField setValue:[current objectAtIndex:0] forKey:@"fieldName"];
                    [newField setValue:[NSNumber numberWithDouble:[[current objectAtIndex:1] doubleValue]] forKey:@"value"];
                }
                NSError *error;
                if (![context save:&error]) 
                {
                    NSLog(@"Couldn't save: %@", [error localizedDescription]);
                }
                [self storeArray];
                [self.tableView reloadData];
            }
        }
    }
    input.text = nil;
}

原谅奇怪的开始和结束括号缩进。:/

无论如何,这就是我从用户那里获取输入并在本地访问文件的代码,我相信你们已经意识到了。现在我想知道如何在我的服务器中获取文件的路径。

另外,如果你们发现任何其他错误,例如写作风格和其他不良习惯,请告诉我,因为我是 iOS 新手。

非常感谢您!如果您不理解我的问题,请澄清,因为我有时不善于解释自己!:)

4

1 回答 1

2

正如我猜测的那样,您正在尝试从服务器的 .csv 文件中获取数据,并希望在表格视图列表中显示该数据。

所以我建议您尝试在 NSData 中获取该 .csv 文件数据,然后进行处理。

NSData *responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"serverUrl"]];
NSString *csvResponseString = [[[NSString alloc] initWithData:responseData   encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"responseString--->%@",csvResponseString);

现在尝试使用 nsstring 的方法 ( componentsSeparatedByString) 和 coma (')

arrSepratedData = [[responseString componentsSeparatedByString:@","];

现在使用这个 arr 来UITableView填充数据。

于 2012-07-12T11:48:17.250 回答