【问题标题】:Problems populating UITableView with data from XML file使用 XML 文件中的数据填充 UITableView 的问题
【发布时间】:2011-01-11 17:06:59
【问题描述】:

我会尽快解释我的问题是什么......

我正在加载一个 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 数据中有一些德语 umlauts,但我定义了 UTF-8 编码,所以这应该不是问题。但如果是,请告诉我一个修复。或者其他任何可能的问题......

【问题讨论】:

  • 应用程序崩溃。如果注释掉最后两行 NSString *str... 一切正常!

标签: iphone xml uitableview diacritics


【解决方案1】:

我认为您在cellForRowAtIndexPath 方法中重新声明了Exposition *e 变量。在 .h 文件中声明该变量。

【讨论】:

  • 试过但没用。老实说,我也不明白为什么在 cellForRowAtIndexPath 中声明这个变量时它不应该工作。我只是无法从这个实例中获取数据,这似乎使应用程序崩溃。为什么?
  • 修改该行如下,如果你在cellForRowAtIndexPath方法中写这行:NSString *str = [[e objectAtIndex:i] name];
【解决方案2】:

我现在刚刚使用NSXMLParser,一切正常,即使是变音符号。

由于 XML 文件的文件大小无论如何都会很小,因此与 touchXML 库相比,性能应该没有那么重要。

感谢那些试图提供帮助的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    相关资源
    最近更新 更多