この簡略化されたxmlファイルのNタグからすべての属性「unit1」と「unit2」を解析し、それらを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>
私はNSXMLParser、自己委任を使用しており、次のコードを使用しています。
- (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];
}
}
UITableViewにvalue1とvalue2を入力するには、次のようにします。
- (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;
}
解析は完璧です。N個のstringValuesごとにNSLogを実行することもできますが、UITableViewには、arrayUnit Nの最後の値(この例ではunit1TimeNとunit2TimeN)を含む1行しか表示されません。では、配列のすべてのN値であるすべての値をテーブルに入力するにはどうすればよいですか?たぶん私も実装する必要があります-(void)parser:(NSXMLParser *)parser didEndElement :?ありがとう!