我正在使用XML解析器從博客獲取信息以創建饋送閱讀器應用程序。我創建了一個對象,其屬性是每個博客條目的數據(標題,發佈,作者...)。我將數據存儲在對象中,然後使用指針將對象放入解析數據的數組中。當我去訪問屬性以在我的UITableView中顯示它們時,每個單元格都是相同的,每個單元格的最後一個博客條目的數據都是相同的。訪問數組中的對象的屬性以在UITableViewCell中顯示
解析器.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的值,並且兩者都是正確的。 我在想什麼?
你有什麼錯誤? – Dany 2013-02-12 05:07:50
您在哪裏以及如何創建feedDataFromParser?如果你記錄它,它是否只顯示一個對象? – rdelmar 2013-02-12 05:08:06
@VishnuPrasath沒有錯誤。問題是,當我運行模擬器時,該表格具有適當數量的單元格(等於解析的博客條目數),但它們都是相同的(即解析了最後一篇博客條目)。 – mbrauh 2013-02-12 05:10:54