我正在使用XML解析器从博客中获取信息以创建供稿阅读器应用程序。我创建了一个对象,其属性是每个博客条目的数据(标题,已发布,作者...)。我将数据存储在对象中,然后使用指针将对象放入已解析数据的数组中。当我访问属性以在我的UITableView中显示它们时,每个单元格都是相同的,每个单元格的最后一个博客条目的数据。
解析器.m文件
@interface Parser()
//This property holds the blog objects that were parsed
@property (nonatomic, strong) NSMutableArray *parsedResults;
//This property holds the current element content being parsed
@property (nonatomic, strong) NSString *currentElement;
@property (nonatomic, strong) FRFeedItem *blogEntry;
@end
@implementation SolsticeParser
@synthesize parsedResults = _parsedResults;
@synthesize currentElement = _currentElement;
// Will be used to truncate data parsed from publish tag so that it will only store the YYYY-MM-DD to self.blogEntry.datepublished
NSRange dateOnly = {0, 10};
//This method initializes the parser, sets the delegate, starts parsing, and returns the results.
- (NSMutableArray *)parseFeedWithResults:(NSURL *)URL
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
parser.delegate = self;
self.parsedResults = [[NSMutableArray alloc] init];
[parser parse]; // Everything parsed here
return self.parsedResults;
}
...此处已解析的数据将保存到BlogEntry对象的属性中......
#pragma mark - Parser delegate
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
// Custom blog object initialized here
if ([elementName isEqualToString:@"entry"]) {
if (!self.blogEntry) {
self.blogEntry = [[FRFeedItem alloc] init];
}
}
}
...
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"title"]) {
self.blogEntry.title = self.currentElement;
} else if([elementName isEqualToString:@"published"]) {
self.blogEntry.datePublished = [self.currentElement substringWithRange:dateOnly];
} else if([elementName isEqualToString:@"entry"]) {
[self.parsedResults addObject:self.blogEntry];
}
}
在MyTableViewController.m中:
@interface MyTableViewController ()
@property (nonatomic, strong) Parser* parser;
@property (nonatomic, strong) NSMutableArray* feedDataFromParser;
@end
@implementation MyTableViewController
// synthesize automatically done by Xcode v4.6
- (void)viewDidLoad
{
[super viewDidLoad];
self.parser = [[Parser alloc] init]; // initialize parser by allocating memory on the heap
[self loadItems]; // automatically loads data to be displayed upon opening the app
}
- (void)loadItems
{
// information parsed from blog stored to a mutable array
self.feedDataFromParser = [self.parser parseFeedWithResults:[NSURL URLWithString:kFeedURL]];
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//code not included for this question for brevity
// Configure the cell from data stored in mutable array of FRFeedItem objects
// PROBLEM:
cell.textLabel.text = [[self.feedDataFromParser objectAtIndex:indexPath.row] title];
cell.detailTextLabel.text = [[self.feedDataFromParser objectAtIndex:indexPath.row] datePublished];
return cell;
}
@end
据我所知,语法上没有任何错误。我已经尝试将解析后的数据打印出来并保存到解析器文件中的对象以及indexPath.row的值,两者都是正确的。 我错过了什么?
答案 0 :(得分:0)
我认为问题在于这一行:
if (!self.blogEntry)
创建第一个后,您将不再创建。尝试删除该if子句,看看是否修复了它。