使用 XML 文件中的数据填充 UITableView 时出现问题
我将尝试简短地解释一下我的问题是什么...
我正在加载一个 XML 文件(使用 touchXML)以用数据填充 UITableView。
expositions = [[NSMutableArray alloc] init];
// Get XML string from XML document.
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);
}
}
在 for 循环结束时,您可以看到输出了一些刚刚传递的数据,并且在返回解析的 XML 数据的 NSLog 中运行良好。
访问数组时
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
但是当我尝试像这样
// get exposition data for this row
Exposition *e = [expositions objectAtIndex:indexPath.row];
NSString *str = e.name;
NSLog(@"%@", str);
......我收到错误!我不明白为什么。我在解析的 XML 数据中有一些德语 元音变音 但我定义了 UTF-8 编码,所以这个应该不是问题。但如果是的话,请告诉我解决办法。或者其他任何可能的问题......
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 document.
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在
cellForRowAtIndexPath
方法中重新声明Exposition *e
变量。在 .h 文件中声明该变量。I think you are redeclaring the
Exposition *e
variable incellForRowAtIndexPath
method. Declare that variable in .h file.我现在刚刚使用 NSXMLParser ,一切正常,即使有元音变音。
由于 XML 文件无论如何都会具有较小的文件大小,因此与 touchXML 库相比,性能应该不那么重要。
感谢那些试图提供帮助的人。
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.