【发布时间】:2014-04-05 15:25:03
【问题描述】:
我创建了一个应用程序,它几乎是 Apple 的 SeismicXML 示例应用程序的克隆,以便我可以了解 NSXMLParser 的细节。我创建了自己的.xml 文件,并将大部分(如果不是全部)代码从 SeismicXML 应用程序复制到我的名为 XMLTest 的应用程序中。除了一部分之外,一切似乎都运行良好。当应用程序运行时,所有文本字段都显示相同的数据。无论我设置textField.text显示的解析的哪个属性,它都显示相同的数据,恰好是最后一个被解析的元素。
在调试过程中,我进入了解析操作,并实现了许多NSLog 语句,以查看正在解析的数据。所有NSLog 语句都显示正在解析.xml 文件的正确元素并将其设置为正确对象的正确属性。
我不确定我的代码的哪一部分是错误的,而且我已经自己调试这个应用程序太多小时了。我已经附上了我认为可能是问题的罪魁祸首的部分代码。
在这段代码 sn-p 中,两个标签的值是相同的,这是不应该的。
@implementation VTSVehicleTableViewCell
-(void)configureWithVehicle:(VTSVehicle *)vehicle {
self.stockLabel.text = vehicle.stock;
self.infoLabel.text = vehicle.year;
}
这是在模拟器中生成的:
但是,就在这个方法内部,它在控制台中生成了以下正确信息:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (_accumulatingParsedCharacterData) {
// If the current element is one whose content we care about, append 'string'
// to the property that holds the content of the current element.
//
NSLog(@"%@", string);
[self.currentParsedCharacterData appendString:string];
NSLog(@"%@", string);
}
}
就像车辆对象在解析 xml 期间和之后在其中包含正确的数据,但是当我去更新视图时,车辆对象中的数据现在只填充最后一个内部的数据xml 文件的元素。这是 xml 文件的样子:
<vehicles published="2014-04-02">
<vehicle>
<stock>003737</stock>
<serial>WVWDM7AJ2BW166848</serial>
<year>2011</year>
<make>VOLKSWAGEN</make>
<model>GOLF</model>
<date>01/05/11</date>
<price>26540.00</price>
<invoice>25414.00</invoice>
<trim>4DR TDI 6SP AUT</trim>
</vehicle>
<vehicle>
<stock>003974</stock>
<serial>3VWLL7AJ1CM317028</serial>
<year>2012</year>
<make>VOLKSWAGEN</make>
<model>JETTA</model>
<date>11/03/11</date>
<price>0.00</price>
<invoice></invoice>
<trim>2.0L TDI 6SP AUTO</trim>
</vehicle>
<vehicle>
<stock>004104</stock>
<serial>1VWBH7A38CC061891</serial>
<year>2012</year>
<make>VOLKSWAGEN</make>
<model>PASSAT</model>
<date>03/05/12</date>
<price>26665.00</price>
<invoice>25555.00</invoice>
<trim>SE W/SR 2.5 6SP AUTO</trim>
</vehicle>
</vehicles>
为什么文本标签显示相同的数据,即使它们来自同一对象的不同属性?
编辑:这是cellForRowAtIndexPath的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kVehicleCellID = @"VehicleCellID";
VTSVehicleTableViewCell *cell = (VTSVehicleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:kVehicleCellID];
// Get the specific vehicle for this row.
VTSVehicle *vehicle = (self.vehicleList)[indexPath.row];
[cell configureWithVehicle:vehicle];
return cell;
}
这是tableViewCell的代码:
#import "VTSVehicleTableViewCell.h"
#import "VTSVehicle.h"
@interface VTSVehicleTableViewCell ()
// References to the subviews which display the earthquake data.
@property (nonatomic, weak) IBOutlet UILabel *stockLabel;
@property (nonatomic, weak) IBOutlet UILabel *infoLabel;
@end
@implementation VTSVehicleTableViewCell
-(void)configureWithVehicle:(VTSVehicle *)vehicle {
self.stockLabel.text = vehicle.stock;
self.infoLabel.text = vehicle.year;
}
@end
【问题讨论】:
-
我怀疑您被否决了,因为您将代码放在了外部站点上。请编辑您的问题以在您的问题中包含相关部分。请参阅 Stack Overflow 帮助系统的 How do I ask a good question 部分的“如何帮助他人重现您的问题”,其中指出您应该在问题中包含代码示例。
-
谢谢@Rob。我编辑了我的问题。
-
会不会是你解析得很好但实现 UITableView(和/或其模型数据)错误?
-
问题可能出在
cellForRowAtIndexPath中,正如 matt 建议的那样,或者更有可能出现在didEndElement或didStartElement中,重复使用您的NSMutableString,而忽略了copy的值。你能在那些初始化和/或保存currentParsedCharacterData的方法中向我们展示相关的sn-ps吗? -
我已经添加了来自 cellForRowAtIndexPath 的代码以供参考。
标签: objective-c xml parsing nsxmlparser