我一直在這個工作幾個小時,現在已經成爲難倒。我可能做了一些冗餘或愚蠢的事情,導致這個問題,所以我可以得到任何幫助將不勝感激。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
該問題實際上與cellForRowAtIndexPath方法有關。當試圖填充單元格時,我正在使用一個普通的NSString對一個不允許的NSMutableArray對象。修改後的代碼如下: NSString * cellValue = [NSString stringWithFormat:@「%@」,_podAllEntries]; cell.textLabel.text = cellValue; 這填充一行,但不是全部。與以前相比,這是一個改進,因爲應用程序可能會崩潰或根本不顯示任何內容。如果我陷入困境,我會創建新的問題線程。 – Fostenator 2012-04-12 16:34:13