I am currently receiving a file and storing it into an NSString. I am then creating an array out of the string and presenting it in a TableView. This works to a certain extent. I am currently receiving data like this:
CompanyName|AccountCode\r\nCompanyName|AccountCode\r\nCompanyName|AccountCode\r\n and so on...
at the moment i am doing:
NSString *dataString = [NSString stringWithContentsOfURL:url encoding:nil error:nil];
myArray = [dataString componentsSeparatedByString:@"\r\n"];
which displays data as:
CompanyName|AccountCode CompanyName|AccountCode CompanyName|AccountCode
My question is: Can i split the "dataString" into a 2 dimensional array? or should i create 2 arrays (one with the CompanyName, one with AccountCode). And how do i do that?
Thanks
EDIT:
// 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;
}
EDIT:
for test purposes my code is:
- (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;
}
Marzapower is correct, you are probably going to want to use an NSDictionary or NSMutableDictionary to store key value pairs. Right below your code above, you could use this code:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *s in myArray)
{
NSArray *arr = [s componentsSeparatedByString:@"|"];
[dict setObject:[arr objectAtIndex:1] forKey:[arr objectAtIndex:0]];
}
NSLog(@"Dictionary: %@", [dict description]);
NSLog(@"Account code for CompanyName1: %@", [dict objectForKey:@"CompanyName1"]);
The logging code shows the resulting dictionary as well as how to extract an object based on a company name. Please keep in mind that there is no error checking here, if there is no pipe character in one of the array components the setObject line will blow up.
And of course if you want the account code as the key, just flip around the 1 and 0 in the setObject line.
EDIT: In the cellForRowAtIndexPath you should be accessing the dict dictionary instead of the testArray2 array. For example, you would probably want to do something like this:
cell.textLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [dict objectForKey:cell.textLabel.text];
(I hope this is right, I do not have a way to immediately test it in Xcode.)
You can create an associative array, also called "dictionary". With the dictionary you can associate a string (value) to another (key). This way you will obtain something like this:
"Company1" => "account code 1",
"Company2" => "account code 2",
...
Then you could iterate through its keys with the allKeys
method.
Please, refer to the NSMutableDictionary
documentation for further information.
精彩评论