简体   繁体   中英

Parsing a two dimensional array in Objective C

I have made an iOS application that is parsing a single dimension array that populates a UITableView, i tried to send two entries to the array the "Name" of the File and the "URL" of the file from the xml. But in the Table view got populated with the Name and the URL. i want to display the name as the cell text and the url as the cell detail text. Any help?

 (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"%@",string);
if(count){
    listset1=[[NSMutableArray alloc]initWithCapacity:20];
    count=0;
}


if ([currentElementValue isEqualToString:@"Name"]) {
    [Name appendString:string];
    [listset1 addObject:[NSString stringWithFormat:@"%@",Name]];
        //    NSLog(@"%@",listset1);
}

if ([currentElementValue isEqualToString:@"URL"]) {
    [URL appendString:string];
    [listset1 addObject:[NSString stringWithFormat:@"%@",URL]];
    //NSLog(@"%@",URL);

}

Listset1 is the array that is being parsed.

It is not possible in objective-c language.

Either you take two arrays or refer "Class" structure.

I'll prefer you class structure.

Create a class type of NSObject with two objects as NSString for Name and URL .

While getting value,

sampleClass *objClass = [[sampleClass alloc] init];

objClass.strName = [NSString stringWithFormat:@"%@", strName];
objClass.strURL = [NSString stringWithFormat:@"%@", strURL];

[listArray addObject: objClass];

While displaying data,

for (int i = 0; i < [listArray count]; i++)
{
   objClass = [listArray objectAtIndex:i];

   NSLog(@" --> %@", objClass.strName);
   NSLog(@" --> %@", objClass.strURL);
}

Hope, you'll understand.

Thanks.

you store name and url in NSMutableDictionary and store in NSMutableArray

  -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

        if(count){
           NSMutableArray* listset1=[[NSMutableArray alloc]initWithCapacity:20];
            count=0;
        }

         NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        if ([currentElementValue isEqualToString:@"Name"]) {
            [Name appendString:string];
            [dict setValue:[NSString stringWithFormat:@"%@",Name] forKey:@"Name"];
        }

        if ([currentElementValue isEqualToString:@"URL"]) {
            [URL appendString:string];
            [dict setValue:[NSString stringWithFormat:@"%@",URL] forKey:@"URL"];

        }
        [listset1 addObject:dict];
        [dict release];
    }

A multidimensional array isn't a possibilty.

Let's Assume that you have two different arrays and you have preserved the semantics between the two. So name[0] corresponds to url[0]

Now implement the following method in your viewcontroller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *CellIdentifier;
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textlabel.text=name[indexPath.row];
cell.detailTextLabel.text=url[indexPath.row];

return cell;


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM