Problems populating UITableView with data from XML file
I will try to explain shortly what my problem is...
I'm loading an XML file (using touchXML) to populate a UITableView with data.
expositions = [[NSMutableArray alloc] init];
// Get XML string from XML do开发者_如何学JAVAcument.
NSString *xmlURL = EXPOSITION_XML_DOC;
NSString *xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:xmlURL]];
NSString *xmlStringUTF8 = [NSString stringWithUTF8String:[xmlString UTF8String]];
CXMLDocument *xmlDoc = [[CXMLDocument alloc] initWithXMLString:xmlStringUTF8 options:0 error:nil];
// Parse XML document.
NSArray *expos = [xmlDoc nodesForXPath:@"/expositions/exposition" error:nil];
if (expos != nil && [expos count] >= 1)
{
// create Exposition instances with data from xml file
for (CXMLElement *expo in expos)
{
NSString *date = [[[[expo attributeForName:@"date"] stringValue] copy] autorelease];
NSString *name = [[[[expo attributeForName:@"name"] stringValue] copy] autorelease];
NSString *place = [[[[expo attributeForName:@"place"] stringValue] copy] autorelease];
NSLog(@"hello , %@, %@, %@", date, name, place);
Exposition *e = [[Exposition alloc] initWithDate:date name:name place:place];
[expositions addObject:e];
NSLog(@"%@", e.name);
}
}
At the end of the for loop, you can see that output some of the data just passed and it works fine in NSLog returning the parsed XML data.
But when I try to access the array in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
like so...
// get exposition data for this row
Exposition *e = [expositions objectAtIndex:indexPath.row];
NSString *str = e.name;
NSLog(@"%@", str);
I get an error! And I don't understand why. I have some German umlauts in the parsed XML data but I defined the UTF-8 encoding so this should not be a problem. But if it is, please tell me a fix. Or whatever else could be the problem...
I think you are redeclaring theExposition *e
variable in cellForRowAtIndexPath
method. Declare that variable in .h file.
I just used NSXMLParser
now and everything works fine, even with umlauts.
Since the XML file is going to be of small file size anyway, the performance in contrast to the touchXML library should not matter that much.
Thx to the people that tried to help.
精彩评论