2012-04-12 9 views
0

私はこれを数時間前から取り組んできました。私はおそらく、この問題の原因となっている何か冗長か愚かなことをやっているので、私が得ることができるどんな助けも大いに感謝するでしょう。iOS:NSXMLParser - 属性値をUITableViewに追加する

NSURLを使用してウェブサイトを取得しています:http://undignified.podbean.com/feedここからは、NSXMLParserを使用してXMLページを解析し、elementName "enclosure"を検索しています。 "enclosure"要素には、 "url"という属性が含まれています。この属性には、Mp3のURLが含まれています。解析部分が機能し、配列の内容をNSログアウトすることができます。利用可能なすべてのURLが存在することを確認しましたが、これをUITableViewにロードすることはできません。 _podAllEntries内の解析された属性値をロードする私の配列は、dispatch_async内の値でコンソールにログを記録しますが、ViewControllerの他の部分がこの配列にアクセスすると空白になります。私は非同期は、バックグラウンドスレッドで呼び出されている可能性がありますので、他のメソッドがこの配列の値にアクセスできるように、ある種のデリゲートを実装する必要があると思いますか?私は完全に間違っているかもしれません。

要約すると、私はRSSフィード(XMLページ)に接続しようとしていますが、 "url"という属性を解析してその値を集め、URLをtableViewにロードします。私のコードは以下の通りです。私が何かを不明瞭または不明瞭にしている場合は、お気軽にコメントしてください。

UnDigParser.h - クラス

@interface UnDigParser : NSXMLParser <NSXMLParserDelegate> { 
} 
@property (retain) NSMutableArray *links; 
@end 

http://undignified.podbean.com/feedを解析するために使用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 
+0

問題は実際にはcellForRowAtIndexPathメソッドに関連しています。私は、NSMutableArrayオブジェクトに対してプレーンなNSStringを使用していたセルを移入しようとしましたが、これは許可されていません。次のコードを変更しました。 NSString * cellValue = [NSString stringWithFormat:@ "%@"、_podAllEntries]; cell.textLabel.text = cellValue; これは行を入力しますが、すべての行を入力するわけではありません。これはアプリケーションがクラッシュするか、まったく何も表示しないため、以前より改善されています。私が困惑したら、新しい質問スレッドを作成します。 – Fostenator

答えて

0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

これを追加してみます:)

+0

ありがとう:)私はそれを逃していたが、残念ながらテーブルはまだ空白です。 – Fostenator

+0

テーブルビューのデリゲート(2人)がデリゲートを設定しましたか? – Manuel

+0

たぶん...説明できますか? – Fostenator

0

問題が実際にcellForRowAtIndexPathメソッドに関連していました。私は、NSMutableArrayオブジェクトに対してプレーンなNSStringを使用していたセルを移入しようとしましたが、これは許可されていません。次のコードに変更されました:

NSString *cellValue = [NSString stringWithFormat:@"%@", _podAllEntries]; 

cell.textLabel.text = cellValue; 
関連する問題