-1

I'm having a problem with RSS feeds in a table view in an iOS app. I originally tested the RSS feed in it's project with the table view as the root view. I'm trying to get the same functionality in a different project but the table view that displays the list of articles from the RSS feed is blank. The code for the table view in the new project is the same. The only different is that I have a different root view controller that has a bunch of buttons. One button is supposed to go to that table view and it does but the rows are empty. I'm thinking this may be an issue with how the root view controller is set up since I know the table view is populated when that code is run by itself. Here are the AppDelegate files where it sets the root view controller.

//
//  KFBAppDelegate.h
//  KFBNewsroom
//
//  Created by KFB on 10/15/12.
//  Copyright (c) 2012 com.kfb. All rights reserved.
//

#import <UIKit/UIKit.h>

@class KFBViewController;

@interface KFBAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) KFBViewController *viewController;

@end


//
//  KFBAppDelegate.m
//  KFBNewsroom
//
//  Created by KFB on 10/15/12.
//  Copyright (c) 2012 com.kfb. All rights reserved.
//

#import "KFBAppDelegate.h"
#import "KFBViewController.h"
#import "ListViewController.h"
#import "WebViewController.h"
#import "ActionAlertsViewController.h"
#import "MarketUpdatesViewController.h"

@implementation KFBAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[KFBViewController alloc] initWithNibName:@"KFBViewController"  bundle:nil];
ListViewController *lvc = [[ListViewController alloc]initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[lvc setWebViewController:wvc];
ActionAlertsViewController *avc = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
[avc setWebViewController:wvc];
MarketUpdatesViewController *mvc = [[MarketUpdatesViewController alloc]initWithStyle:UITableViewStylePlain];
[mvc setWebViewController:wvc];
self.window.rootViewController = self.viewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}






    //
    //  ListViewController.h
    //  Nerdfeed
    //
    //  Created by KFB on 10/16/12.
    //  Copyright (c) 2012 com.kfb. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    // @interface ListViewController : NSObject

    // a forward declaration; we'll import the header in the .m
    @class RSSChannel;
    @class WebViewController;

    @interface ListViewController : UITableViewController
    <NSXMLParserDelegate>
    {
        NSURLConnection *connection;
        NSMutableData *xmlData;
        RSSChannel *channel;
    }
    @property (nonatomic, strong)WebViewController *webViewController;

    - (void)fetchEntries;


    @end





//
//  ListViewController.m
//  Nerdfeed
//
//  Created by KFB on 10/16/12.
//  Copyright (c) 2012 com.kfb. All rights reserved.
//

#import "ListViewController.h"
#import "RSSChannel.h"
#import "RSSItem.h"
#import "WebViewController.h"

@implementation ListViewController
@synthesize webViewController;

- (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"])
    {
        // If the parser saw a channel, create new instance, store in our ivar
        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
        [parser setDelegate:channel];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // return 0;

    return [[channel items]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    // return nil;

    UITableViewCell *cell = [tableView   dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    RSSItem *item = [[channel items]objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:[item title]];

    return cell;
}

- (void)fetchEntries
{
    // Create a new data container for the stuff that comes back from the service
    xmlData = [[NSMutableData alloc]init];

    // Construct a URL that will ask the service for what you want -
    // note we can concatenate literal strings together on multiple lines in this way -  this results in a single NSString instance
    NSURL *url = [NSURL URLWithString:@"http://kyfbnewsroom.com/category/public- affairs/feed"];

    // Put that URL into an NSURLRequest
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Create a connection that will exchange this request for data from the URL
    connection = [[NSURLConnection alloc]initWithRequest:req delegate:self     startImmediately:YES];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];

    if (self)
    {
        [self fetchEntries];
    }

    return self;
}

// This method will be called several times as the data arrives
- (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
{
    /* We are just checking to make sure we are getting the XML
     NSString *xmlCheck = [[NSString alloc]initWithData:xmlData     encoding:NSUTF8StringEncoding];
     NSLog(@"xmlCheck = %@", xmlCheck);*/

    // Create the parser object with the data received from the web service
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];

    // Give it a delegate - ignore the warning here for now
    [parser setDelegate:self];

    //Tell it to start parsing - the document will be parsed and the delegate of NSXMLParser will get all of its delegate messages sent to it before this line finishes execution - it is blocking
    [parser parse];

    // Get rid of the XML data as we no longer need it
    xmlData = nil;

    // Reload the table.. for now, the table will be empty
    [[self tableView]reloadData];

    NSLog(@"%@\n %@\n %@\n", channel, [channel title], [channel infoString]);
}

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
{
    // Release the connection object, we're done with it
    connection = nil;

    // Release the xmlData object, we're done with it
    xmlData = nil;

    // Grab the description of the error object passed to us
    NSString *errorString = [NSString stringWithFormat:@"Fetch failed: %@", [error localizedDescription]];

    // Create and show an alert view with this error displayed
    UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [av show];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Push the web view controller onto the navigation stack - this implicitly creates the web view controller's view the first time through
    [[self navigationController]pushViewController:webViewController animated:YES];

    // Grab the selected item
    RSSItem *entry = [[channel items]objectAtIndex:[indexPath row]];

    // Construct a URL with the link string of the item
    NSURL *url = [NSURL URLWithString:[entry link]];

    // Construct a request object with that URL
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    // Load the request into the web view
    [[webViewController webView]loadRequest:req];

    // Set the title of the web view controller's navigation item
    [[webViewController navigationItem]setTitle:[entry title]];
}



@end

3 Answers 3

0

Your ListViewController class must inherit from UITableViewController

Edit: Sorry I didn't see it, just add the delegate & dataSource

@interface ListViewController : UITableViewController <NSXMLParserDelegate, UITableViewDelegate, UITableViewDataSource>

Did you add:

-(void)viewDidLoad {
[super viewDidLoad];
[tableView setDelegate: self];
[tableView setDataSource: self];

}

On implementation of ListViewController (Like after @synthesize webViewController; )?

9
  • I did all of that and get No known class method for selector 'setDataSource' and No known class method for selector 'setDelegate'
    – RagingGoat
    Oct 22, 2012 at 22:35
  • Actually, when it's put in just like above, I get Unknown receiver 'tableView', did you mean UITableView?
    – RagingGoat
    Oct 22, 2012 at 23:05
  • I got that code to work but the rows in the table view are still empty.
    – RagingGoat
    Oct 23, 2012 at 18:32
  • I added NSLog(@"%u", [[channel items]count]; right below return [[channel items]count]; and don't get anything in the console.
    – RagingGoat
    Oct 24, 2012 at 10:55
  • Try NSLog("%d", [[channel items] count]);
    – Solidus
    Oct 24, 2012 at 11:52
0

Make sure you set up the UITableView DataSource so that you can actually display rows. You'll probably need to set the delegate too.

EDIT: You're still not setting the UITableView delegate or datasource. You need to do that so that the UITableView knows where to get data from. I think the best place for you to put that is in your ListViewController's viewDidLoad method

-(void)viewDidLoad {
    [super viewDidLoad];
    [tableView setDelegate: self];
    [tableView setDataSource: self];
}

You'll also need to add the UITableViewDelegate and UITableViewDataSource protocols to ListViewController.

EDIT 2:

Make sure you're adding the UITableViewDataSource and UITableViewDelegate protocols to ListViewController

@interface ListViewController : UITableViewController
<NSXMLParserDelegate, UITableViewDelegate, UITableViewDataSource>

Also, make sure the viewDidLoad method above is in the ListViewController class. All UITableViewControllers have a tableView property that holds the actual UITableView.

9
  • Here is the view controller for the table view:
    – RagingGoat
    Oct 22, 2012 at 18:38
  • I'm getting an error when I add that. "No known class method for selector 'setDataSource'" and "No know class method for selector 'setDelegate'"
    – RagingGoat
    Oct 22, 2012 at 19:32
  • Then whatever you are calling those selectors on is not the UITableView. Make sure you are calling the UITableView, not the UITableViewController.
    – hukir
    Oct 23, 2012 at 3:52
  • I'm putting it in ListViewController
    – RagingGoat
    Oct 23, 2012 at 12:31
  • @interface ListViewController : UITableViewController <NSXMLParserDelegate>
    – RagingGoat
    Oct 23, 2012 at 13:00
0

Try to remove this line from ListViewController.h

 @property (nonatomic, retain) UITableView *tableView;

Then replace your - (void) viewDidLoad in ListViewController.m by this code:

- (void) viewDidLoad
{
   [super viewDidLoad];
   [self.tableView setDelegate: self];
   [self.tableView setDataSource:self];
}
9
  • The table view is still blank
    – RagingGoat
    Oct 24, 2012 at 16:37
  • So, I placed a breakpoint at tableView:cellForRowAtIndexPath: which was another suggestion I received. The app continued to run after hitting the button to go to that view so that part of code isn't even being executed.
    – RagingGoat
    Oct 24, 2012 at 18:41
  • I suggest you to restart your project and use storyboard, it's more simple and you'll not have problems as yours
    – Solidus
    Oct 25, 2012 at 14:50
  • If I change - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return 1; instead of return [[channel items]count]; then tableView:cellForRowAtIndexPath: gets called but obviously the table is still empty.
    – RagingGoat
    Oct 25, 2012 at 17:18
  • I've also added NSLog(@"channel items %d", [[channel items]count]; and get this 2012-10-25 13:31:28.916 Newsroom[3457:c07] channel items 10
    – RagingGoat
    Oct 25, 2012 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.