2012-04-12 41 views
0

我一直在这个工作几个小时,现在已经成为难倒。我可能做了一些冗余或愚蠢的事情,导致这个问题,所以我可以得到任何帮助将不胜感激。iOS:NSXMLParser - 将属性值添加到UITableView

我正在使用NSURL拉入网站:http://undignified.podbean.com/feed。从这里我使用NSXMLParser来解析XML页面并搜索elementName“enclosure”。 “enclosure”元素包含属性“url”,其中包含Mp3的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 
+0

该问题实际上与cellForRowAtIndexPath方法有关。当试图填充单元格时,我正在使用一个普通的NSString对一个不允许的NSMutableArray对象。修改后的代码如下: NSString * cellValue = [NSString stringWithFormat:@“%@”,_podAllEntries]; cell.textLabel.text = cellValue; 这填充一行,但不是全部。与以前相比,这是一个改进,因为应用程序可能会崩溃或根本不显示任何内容。如果我陷入困境,我会创建新的问题线程。 – Fostenator 2012-04-12 16:34:13

回答

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

尝试添加这个:)

+0

谢谢:)。我错过了这一点,但不幸的是桌子仍然是空白的。 – Fostenator 2012-04-12 14:36:47

+0

您是否为桌面视图设置了代表(有2个)委托? – Manuel 2012-04-12 14:37:45

+0

也许不......你能解释一下吗? – Fostenator 2012-04-12 14:40:16

0

这个问题实际上是相关的cellForRowAtIndexPath方法。当试图填充单元格时,我正在使用一个普通的NSString对一个不允许的NSMutableArray对象。修改后的代码如下:

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

cell.textLabel.text = cellValue;