私が直面している問題は、URL パスを含む _podAllEntries というラベルの付いた NSMutableArray があることです。この配列の内容は、UITableView の 1 つの行に読み込まれます。この配列には合計 4 つの項目があり、項目ごとにテーブルにエントリが必要です。以下は、アプリの仕組みのより詳細な説明です。
この iOS アプリは、NSURL を使用して Web サイト ( http://undignified.podbean.com/feed ) から XML データを使用します。そこから、NSXMLParser を使用して XML を解析し、「enclosure」要素内の「url」という属性を検索します。次に、「url」に一致するすべての属性が配列に追加され、各項目が個別の行にある UITableView に読み込まれる必要があります (前述のように、1 行だけが入力されています)。
私はおそらくこの時点で些細なことを見逃しているので、正しい方向への微調整やフィードバックをいただければ幸いです。
UnDigParser.h
クラスを解析するためのヘッダー ファイル:
#import <Foundation/Foundation.h>
@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> {
}
@property (retain) NSMutableArray *links;
@end
UnDigParser.m
解析クラスの実装:
#import "UnDigParser.h"
@implementation UnDigParser
NSMutableArray *_links;
@synthesize links = _links;
-(void)parserDidStartDocument:(NSXMLParser *)parser{
_links=[[NSMutableArray alloc] init];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"enclosure"]){
NSString *link = [attributeDict objectForKey:@"url"];
if (link){
[_links addObject:link];
}
}
//NSLog(@"%@",_links);
}
-(BOOL)parse{
self.delegate = self;
return [super parse];
}
@end
ViewController.h
@class WebViewController;
@interface getpodViewController : UITableViewController <UITableViewDataSource>
{
NSMutableArray *_podAllEntries;
NSMutableArray *_allEntries;
WebViewController *_webViewController;
}
@property(retain) NSMutableArray *podAllEntries;
@property(retain) NSMutableArray *allEntries;
@property(retain) WebViewController *webViewController;
@end
ViewController.m
@implementation getpodViewController
@synthesize podAllEntries = _podAllEntries;
@synthesize allEntries = _allEntries;
@synthesize webViewController = _webViewController;
-(void)addRows
{
//dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURL *url = [NSURL URLWithString:@"http://undignified.podbean.com/feed"];
UnDigParser *parser = [[UnDigParser alloc] initWithContentsOfURL:url];
[parser parse];
[_podAllEntries insertObject:parser.links atIndex:0];
NSLog(@"%@", _podAllEntries);
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Podcasts";
self.podAllEntries = [[NSMutableArray alloc]init];
[self addRows];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_podAllEntries 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];
}
NSString *cellValue = [NSString stringWithFormat:@"%@", _podAllEntries];
cell.textLabel.text = cellValue;
return cell;
}