http://news.yahoo.com/rss/からの最新のRSSフィードを表示するアプリを作成しようとしています 。NSXMLParserDelegateを使用してxmlを解析でき、日付と時刻を一緒に表示できます。特定のフィードのタイトルですが、すべてのフィードの画像も表示したいので、主要な要素を解析する方法は知っていますが、URLをキーとして持つ子要素の属性を渡す方法がわかりません...たとえば、上記のフィードは次のもので構成されています。
<item>
<title>fooo</title>
<description ahref="some link" image src="http://l1.yimg.com/bt/api/res/1.2/Wo3_apH.kz7DMvOj7MDtRQ--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/20c464ff99815420210f6a706700a792.jpg"
</description>
テーブルビューにIMGSRCの画像と、その特定のフィードのタイトルと日付が必要です。IMG SRC属性を解析し、タイトルと日付を乱さないようにサイズを変更してテーブルビューに追加する方法について混乱しています。
私がこれまでにしたこと:
viewDidAppear
-(void)viewDidAppear:(BOOL)animated
{
if ([stories count] == 0)
{
NSString * path = @"http://news.yahoo.com/rss/";
[self parseXMLFileAtURL:path];
}
cellSize = CGSizeMake([newsTable bounds].size.width, 80);
}
-(void)parseXMLFileAtURL:(NSString *)URL
- (void)parseXMLFileAtURL:(NSString *)URL
{
stories = [[NSMutableArray alloc] init];
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
didStartElement
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
foundCharacters
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
}
didEndElement
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"pubDate"];
}
}
didEndDocument
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[newsTable reloadData];
}
numberOfRowsおよびcellForRowAtIndexPath
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [stories count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
}
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
cell.textLabel.text=[[stories objectAtIndex: storyIndex] objectForKey: @"title"];
cell.detailTextLabel.text=[[stories objectAtIndex:storyIndex] objectForKey:@"pubDate"];
cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
return cell;
}
セルの右側の画像を取得するにはどうすればよいですか?