私はこれに数時間取り組んできて、困惑しました。私はおそらくこの問題を引き起こしている冗長または愚かなことをしているので、私が得ることができるどんな助けも大いに感謝されるでしょう。
NSURLを使用してWebサイトを取得しています:http://undignified.podbean.com/feed。ここから、NSXMLParserを使用してXMLページを解析し、elementName"enclosure"を検索します。「enclosure」要素には、Mp3のURLを含む属性「url」が含まれています。解析部分が機能し、配列の内容をNSLogアウトでき、使用可能なすべてのURLが存在することを確認しましたが、これをUITableViewにロードできず、単に空白としてロードされます。解析された属性値をロードしている私の配列、_podAllEntriesはdispatch_async内の値でコンソールにログを記録しますが、ViewControllerの他の部分がこの配列にアクセスすると空白になります。非同期がバックグラウンドスレッドで呼び出されているので、他のメソッドがこの配列の値にアクセスできるように、何らかのデリゲートを実装する必要があると思いますか?私は完全に間違っているかもしれません。
要約すると、RSSフィード(xmlページ)に接続しようとしています。「url」という属性を解析してその値を収集し、URLをtableViewにロードします。私のコードは以下のとおりです。不明な点やあいまいな点がありましたら、お気軽にコメントしてください。
UnDigParser.h-http://undignified.podbean.com/feedの解析に使用されるクラス
@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> {
}
@property (retain) NSMutableArray *links;
@end
UnDigParser.h-実装ファイル:
#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];
}
}
}
-(BOOL)parse{
self.delegate = self;
return [super parse];
}
@end
ViewController.hファイル:
@interface getpodViewController : UITableViewController <UITableViewDataSource>{
NSMutableArray *_podAllEntries;
NSMutableArray *_allEntries;
}
@property(retain) NSMutableArray *podAllEntries;
@property(retain) NSMutableArray *allEntries;
@end
ViewController.Mファイル:
#import "getpodViewController.h"
#import "PodcastEntry.h"
#import "UnDigParser.h"
@implementation getpodViewController
@synthesize podAllEntries = _podAllEntries;
@synthesize allEntries = _allEntries;
-(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 addObject:parser.links];
NSLog(@"%@", _podAllEntries);
});
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Podcasts";
self.podAllEntries = [[[NSMutableArray alloc]init]autorelease];
[self addRows];
for (UnDigParser *entry in _podAllEntries){
[_allEntries insertObject:entry atIndex:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]
withRowAnimation:UITableViewRowAnimationRight];
}
}
- (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 = [_podAllEntries objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)dealloc {
[super dealloc];
[_podAllEntries release];
_podAllEntries = nil;
}
@end