0

I've been working on this for several hours now and have become stumped. I'm probably doing something redundant or stupid that's causing this issue so any help I can get would be much appreciated.

I'm using NSURL to pull in a website: http://undignified.podbean.com/feed. From here I'm using NSXMLParser to parse the XML page and search for the elementName "enclosure". The "enclosure" element contains an attribute "url" which contains the URL for an Mp3. The parsing part works and I'm able to NSLog out the contents of the array and I've verified that all available URLs are present, but I cannot get this to load into a UITableView, it simply loads as blank. My array that I'm loading the parsed out attribute values in, _podAllEntries will Log to the console with the values within the dispatch_async, but when any other part of my ViewController accesses this array it is blank. I think since the async is being called on a background thread that maybe some sort of delegate has to be implemented so that other methods may access the values in this array? I may be completely wrong though.

To summarize I'm attempting to connect to an RSS feed (xml page), parse for an attribute called "url" and gather the value of it, and then load the URLs into a tableView. My code is listed below. Please feel free to comment if I'm unclear or ambiguous on anything.

UnDigParser.h - Class used to parse http://undignified.podbean.com/feed

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

UnDigParser.h - implementation file:

#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 file:

@interface getpodViewController : UITableViewController <UITableViewDataSource>{
NSMutableArray *_podAllEntries;
NSMutableArray *_allEntries;
}

@property(retain) NSMutableArray *podAllEntries;
@property(retain) NSMutableArray *allEntries;

@end

ViewController.M file:

#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
Fostenator
  • 83
  • 2
  • 12
  • The issue was actually related to the cellForRowAtIndexPath method. When trying to populate the cells I was using a plain NSString against an NSMutableArray object which is not allowed. Modified code to the following: NSString *cellValue = [NSString stringWithFormat:@"%@", _podAllEntries]; cell.textLabel.text = cellValue; This populates a row, but not all of them. Which is an improvement over previously as the application would either crash or not display anything at all. I will create new question thread if I get stumped. – Fostenator Apr 12 '12 at 16:34

2 Answers2

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

Try to add this :)

Manuel
  • 10,153
  • 5
  • 41
  • 60
  • Thanks :). I was missing that, but unfortunately the table is still blank. – Fostenator Apr 12 '12 at 14:36
  • Did you set up a delegate the delegates (there are 2) for the tableview? – Manuel Apr 12 '12 at 14:37
  • Maybe not...Can you explain please? – Fostenator Apr 12 '12 at 14:40
  • Did you add the tableview programmatically or in the interfacebuilder of xcode? – Manuel Apr 12 '12 at 14:41
  • I added it through interface builder, but when I added this portion of the code: [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] I started to get a warning about the "self.tableView" so I had to add the to the ViewController.H file. withRowAnimation:UITableViewRowAnimationRight]; – Fostenator Apr 12 '12 at 14:43
  • ok then you need to rightclick the tableview in the interface builder and drag from the circle behind datasource and delegate to the file's owner (top left) – Manuel Apr 12 '12 at 14:44
  • I checked the table view in interface builder and the dataSource and delegate were already wired to the file's owner. Also the view is wired from the file's owner to the tableView. I deleted the connections and re-wired, but still "no go". – Fostenator Apr 12 '12 at 14:48
  • ok and have you tried not calling the insertrowsatindexpaths: stuff in the viewdidload? that is not the usual way of putting information into a tableview – Manuel Apr 12 '12 at 14:54
  • Yes. I attempted this just now (also tried in previously), but moved it to the -(void)addRows method. Still no luck. Section of code that was moved: for (UnDigParser *entry in _podAllEntries){ [_allEntries insertObject:entry atIndex:0]; //NSLog(@"Found %@",_allEntries); [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight]; – Fostenator Apr 12 '12 at 14:56
  • Im sorry but i can't think of anything else that might cause this right now. – Manuel Apr 12 '12 at 14:58
0

The issue was actually related to the cellForRowAtIndexPath method. When trying to populate the cells I was using a plain NSString against an NSMutableArray object which is not allowed. Modified code to the following:

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

cell.textLabel.text = cellValue;
Fostenator
  • 83
  • 2
  • 12