0

I am downloading and parsing JSON objects to build a "news feed" to populate a UITableView. The very last line of code I have in the connectionDidFinishLoading delegate method is:

[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

However, my break points in the - (UITableViewCell *)tableView:(UITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method are not hit. (They are hit when the app first launches)

So for whatever reason even though I am calling the reloadData on the main thread; it doesn't appear to be firing. I tried just [tableView reloadData] and that did not work.

Here is my connectionDidFinishLoading method:

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

    //NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; 

    NSUInteger newsStreamCount = [publicTimeline count];

    // Initialize the collection and the dictionary
    newsItemManager.newsItemCollection = [[NSMutableArray alloc] initWithCapacity:newsStreamCount];
    NSMutableArray *dictOfNewsItems = [[NSMutableArray alloc] initWithCapacity:newsStreamCount];

    // From the JSON object, parse out the individual news item JSON objects and 
    // put them into a dictionary.
    for (int i = 0; i < newsStreamCount; i++)
    {
        NSDictionary *item = [publicTimeline objectAtIndex:i];
        [dictOfNewsItems addObject:item];
    }

    // For each news item JSON object, extract out the information we need for our
    // news manager class
    for (int i = 0; i < newsStreamCount; i++)
    {
        NSString *userName = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"Title"];
        NSString *message = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"Content"];
        NSString *imgUrl = [[dictOfNewsItems objectAtIndex:i] valueForKey:@"https://si0.twimg.com/logo_normal.jpg"];


        NewsItem *newsItem = [[NewsItem alloc] initWithBasicInfo:userName :message :imgUrl];


        [newsItemManager.newsItemCollection addObject:newsItem];
    }

    [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

Here is my header file:

#import <UIKit/UIKit.h>

@interface NewsViewController : UITableViewController

@property (nonatomic, retain) NSMutableArray* newsItemArray;
@property (nonatomic, retain) NSMutableData *responseData;
@property (nonatomic, retain) IBOutlet UITableView *tableView;

@end

Here is my implementation:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.title = @"News";

    //self.newsItemArray = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];

    tableView.rowHeight = 75.0;


    responseData = [NSMutableData data];        
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://blah.com/api/news"]];
    (void)[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

Thanks, Flea

4
  • 1
    Is tableView nil for some reason at the time you call performSelector on it? NSLog (@" Tableview is %@", tableView);
    – Rayfleck
    Jan 27, 2012 at 22:02
  • Yes Ray, sure seems like it: It printed out: Tableview is (null)
    – Flea
    Jan 27, 2012 at 22:07
  • I'm not sure why this is the case; I am @synthesize at the top of the class.
    – Flea
    Jan 27, 2012 at 22:09
  • I think the problem has to be because this UITableView pointer is null but I am not sure why that is the case.
    – Flea
    Jan 27, 2012 at 22:48

2 Answers 2

2

If your connectionDidFinish... method is inside your tableViewController class, maybe you just need

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
3
  • Please post your .h file where tableview is declared, as well as your init method (or whereever you initialize tableview). Does the table EVER show data?
    – Rayfleck
    Jan 27, 2012 at 22:14
  • Hey Ray, I updated my main question and put my header info and implementation info up there. I was able to get the table to display the contents of a simple array outside of using the connectionDidFinish method.
    – Flea
    Jan 27, 2012 at 22:30
  • Where do you set the tableView's dataSource? It should be newsItemManager.newsItemCollection...
    – Rayfleck
    Jan 27, 2012 at 22:59
1

The overall problem was my newsItemManager.newsItemCollection was not being initialized properly and was returning null the entire time, thus when the UITableView was trying to load data; there was nothing to load.

I thought I had checked for this but one of those problems of staring at the computer all day and missing the obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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