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 に答える