我目前正在接收一个文件并将其存储到NSString中。然后,我将字符串创建为数组,并在TableView中呈现它。这在某种程度上起作用。我目前接收的数据格式如下:
公司名称|账户代码\r\n公司名称|账户代码\r\n公司名称|账户代码\r\n等等...
目前我正在做:
NSString *dataString = [NSString stringWithContentsOfURL:url encoding:nil error:nil];
myArray = [dataString componentsSeparatedByString:@"\r\n"];
这段代码展示的数据如下:
公司名称|账号编码 公司名称|账号编码 公司名称|账号编码
我的问题是:我能将"dataString"拆分为二维数组吗?还是应该创建两个数组(一个包含公司名称,一个包含账号编码)?如何操作?
谢谢。
编辑:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
if (testArray.count >indexPath.row) {
cell.textLabel.text = [testArray2 objectAtIndex:0];
cell.detailTextLabel.text = [testArray2 objectAtIndex:1];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
编辑:
为了测试目的,我的代码是:
- (void)viewDidLoad {
[super viewDidLoad];
testArray = [[NSArray alloc] init];
NSString *testString = @"Sam|26,Hannah|22,Adam|30,Carlie|32";
testArray = [testString componentsSeparatedByString:@","];
dict = [NSMutableDictionary dictionary];
for (NSString *s in testArray) {
testArray2 = [s componentsSeparatedByString:@"|"];
[dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]];
}
NSLog(@"Dictionary: %@", [dict description]);
NSLog(@"Account code for CompanyName1: %@", [dict objectForKey:@"CompanyName1"]);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
if (testArray.count >indexPath.row) {
cell.textLabel.text = [testArray2 objectAtIndex:0];
cell.detailTextLabel.text = [testArray2 objectAtIndex:1];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}