Im searching to parse all the attributes "unit1" and "unit2" from the N tags of this simplified xml file, and then put them in a UITableView:
<xml>
<meta></meta>
<time1>
<product>
<value1 id="id1Time1" unit1="unit1Time1" number1="number1Time1"/>
<value2 id="id2Time1" unit2="unit2Time1" number2="number2Time1"/>
</product>
</time1>
<time2>
<product>
<value1 id="id1Time2" unit1="unit1Time2" number1="number1Time2"/>
<value2 id="id2Time2" unit2="unit2Time2" number2="number2Time2"/>
</product>
</time2>
...
<timeN>
<product>
<value1 id="id1TimeN" unit1="unit1TimeN" number1="number1TimeN"/>
<value2 id="id2TimeN" unit2="unit2TimeN" number2="number2TimeN"/>
</product>
</timeN>
</xml>
I'm using NSXMLParser, self delegate, and I'm using this code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"value1"]) {
arrayUnit1 = [[NSMutableArray alloc] init];
stringUnit1 = [attributeDict objectForKey:@"unit1"];
[arrayUnit1 addObject:stringUnit1];
}
if
([elementName isEqualToString:@"value2"]) {
arrayUnit2 = [[NSMutableArray alloc] init];
stringUnit2 = [attributeDict objectForKey:@"unit2"];
[arrayUnit2 addObject:stringUnit2];
}
}
To populate UITableView with value1 and value2 I'm using:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayUnit1 count]; // <--- I Know, here is the problem!
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",[arrayUnit1 objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[arrayUnit2 objectAtIndex:indexPath.row]];
return cell;
}
Well, the parsing is perfect, I can also NSLog every N stringValues but in my UITableView I can see ONLY ONE row with the LAST values of arrayUnit N (in this example unit1TimeN and unit2TimeN). So how can I populate the table with EVERY value, all the N values of my arrays? Maybe I need to implement also - (void)parser:(NSXMLParser *)parser didEndElement:? Thanks!
value1
in code same asnumber1
in xml? – Eimantas