RSS entries not showing up in the uitableview


#1

So i followed the necessary instructions as they were. The server side logs are showing up in the console but nothing is coming up on the simulator. The RSS feed link that I’m using in the code is— http://forums.bignerdranch.com/smartfeed.php?"@"limit=1_DAY&sort_by=standard&feed_type=RSS2.0&feed_style=COMPACT
Here is my code:

NerdfeedAppDelegate.m

[code]#import “NerdfeedAppDelegate.h”
#import “ListViewController.h”
@implementation NerdfeedAppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    ListViewController *lvc= [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *masterNav= [[UINavigationController alloc] initWithRootViewController:lvc];

    [self.window setRootViewController:masterNav];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    } [/code]

ListViewController.h

#import <UIKit/UIKit.h>
@class RSSChannel;
@interface ListViewController : UITableViewController<NSXMLParserDelegate>
{
    NSURLConnection *connection;
    NSMutableData *xmlData;
    RSSChannel *channel;
}
-(void)fetchEntries; 
@end

ListViewController.m

[code]#import “ListViewController.h”
#import “RSSChannel.h”
#import “RSSItem.h”

@implementation ListViewController

  • (id)initWithStyle:(UITableViewStyle)style
    {
    self = [super initWithStyle:style];
    if (self) {
    // Custom initialization
    [self fetchEntries];
    }
    return self;
    }

    • (void)viewDidLoad
      {
      [super viewDidLoad];

}

-(void)fetchEntries{
xmlData= [[NSMutableData alloc] init];

 NSURL *url= [NSURL URLWithString:@"http://forums.bignerdranch.com/smartfeed.php?"@"limit=1_DAY&sort_by=standard&feed_type=RSS2.0&feed_style=COMPACT"];
NSURLRequest *req= [NSURLRequest requestWithURL:url];
 connection= [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];//self has been made NSURLConnection's delegate

}

-(void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data{
// Add the incoming chunk of data to the container we are keeping
// The data always comes in the correct order
[xmlData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)conn{

 NSXMLParser *parser=[[NSXMLParser alloc] initWithData:xmlData];
 [parser setDelegate:self];
 WSLog(@"parsing initiated");
 [parser parse];

 xmlData=nil;
 connection=nil;
 [[self tableView] reloadData];
 WSLog(@"channel test- %@\n %@\n %@\n",channel, [channel title], [channel infoString]);

}

-(void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error{
connection=nil;
xmlData=nil;
NSString *errorString= [NSString stringWithFormat:@“Fetch failed: %@”,[error localizedDescription]];
UIAlertView *av= [[UIAlertView alloc] initWithTitle:@“Error” message:errorString delegate:nil cancelButtonTitle:@“OK” otherButtonTitles:nil, nil];
[av show];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
  NSLog(@"%@ found a %@ element", self, elementName);
     if ([elementName isEqual:@"channel"]) { //<channel> element starts
    //        If the parser saw a channel, create a new object, have the ivar- 'channel' point to it.
     channel= [[RSSChannel alloc] init];

    //        Give the channel object a pointer back to ourselves for later.
     [channel setParentParserDelegate:self];

    //        Set the parser's delegate to the channel object
    //        There will be a warning here, ignore it for now
     [parser setDelegate:channel];

  }
}

#pragma mark - Table view data source

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [[channel items] count];
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @“Cell”;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell==nil) {
    cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    RSSItem *item= [[channel items] objectAtIndex:[indexPath row]];
    // Configure the cell…
    [[cell textLabel] setText:[item title]];

    return cell;
    }

@end[/code]

RSSChannel.h

[code]#import <Foundation/Foundation.h>

@interface RSSChannel : NSObject{
NSMutableString *currentString;
}
@property (nonatomic, weak)id parentParserDelegate;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *infoString;
@property (nonatomic, readonly, strong) NSMutableArray *items;
@end[/code]

RSSChannel.m

[code]#import “RSSChannel.h”
#import “RSSItem.h”
@implementation RSSChannel
@synthesize items, title, infoString, parentParserDelegate;
-(id)init{
if (self= [super init]) {
// ‘items’ container to store all the RSSItems
items= [[NSMutableArray alloc] init];
}
return self;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

NSLog(@"\t%@ found a %@ element",self,elementName);
if ([elementName isEqual:@"title"]) {
   currentString= [[NSMutableString alloc] init];
   [self setTitle:currentString];
}
else if([elementName isEqual:@"description"]){
  currentString= [[NSMutableString alloc] init];
  [self setInfoString:currentString];
}
else if ([elementName isEqual:@"item"]){
//        When we find an item, create an instance of RSSItem
  RSSItem *entry= [[RSSItem alloc] init];

//        Set up its parent as ourselves so we can regain control of the parser
  [entry setParentParserDelegate:self];

//        Turn the parser to the RSSItem
  [parser setDelegate:entry];

//        Add the item to our array and release our hold on it (this current method would let go of the RSSItem object..)
  [items addObject:entry];
}

}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

  NSLog(@"found character(s)- %@",string);
  [currentString appendString:string];
  NSLog(@"current string- %@",currentString);

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{//Channel element ends
NSLog(@“element ended”);
currentString=nil;
if ([elementName isEqual:@“channel”]) {
[parser setDelegate:parentParserDelegate];
}
}
@end[/code]

RSSItem.h

[code]#import <Foundation/Foundation.h>

@interface RSSItem : NSObject
{
NSMutableString *currentString;
}
@property (nonatomic, weak)id parentParserDelegate;
@property (nonatomic, strong) NSString *title;
@property NSMutableString *link;

@end[/code]

RSSItem.m

[code]#import “RSSItem.h”

@implementation RSSItem
@synthesize link, title,parentParserDelegate;
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

NSLog(@"\t\t%@ found a %@ element", self, elementName);
if ([elementName isEqual:@“title”]) {
currentString= [[NSMutableString alloc] init];
[self setTitle:currentString];
}
else if ([elementName isEqual:@“link”]){
currentString= [[NSMutableString alloc] init];
[self setLink:currentString];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)str{
NSLog(@“found character(s) within item- %@”,str);
[currentString appendString:str];
NSLog(@“link- %@”,link);
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
currentString=nil;
if ([elementName isEqual:@“item”]) {
[parser setDelegate:parentParserDelegate];
}
}
@end[/code]

When i run the app in the iPad simulator , the console shows all the logs that are involved in the execution both server side and client side activities… After the console is done showing all the logs, i wait for the tableview in the app to show some entries… Unfortunately it doesn’t… I don’t understand where the problem is… Pls assist.