0

我正在使用 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的值,两者都是正确的。我错过了什么??

4

1 回答 1

0

我认为问题在于这一行:

if (!self.blogEntry)

创建第一个后,您将不再创建。尝试删除该 if 子句,看看是否可以修复它。

于 2013-02-12T05:33:50.927 回答