views:

178

answers:

1

Hi all,

I am having trouble getting my UITableViewController to reload/refresh the data.

I have a method which uses ASIHTTPRequest to call a URL and bring back some string data. I am trying to put this data into a cell of the table, but [self.tableview reloadData] is not working.

I was thinking that the ASIHTTPRequest completes its tasks on a separate thread, so I tried:

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

which also did nothing.

How can I reload the data? I have been stuck on this for some time now.

Code: MainViewController.h

#import <UIKit/UIKit.h>
@class ProductClass;
@interface MainViewController : UITableViewController {
 ProductClass *item;
}
@property (nonatomic, retain) ProductClass *item;
@end

MainViewController.m

#import "MainViewController.h"
#import "ASIHTTPRequest.h"
#import "ProductClass.h"

@implementation MainViewController
@synthesize item;

- (void) viewDidLoad {
 self.title = @"TableView Test";
 self.tableView.allowsSelection = NO;
 self.item = [[ProductClass alloc] init];
    [self callURL];
}

-(void)callURL {
    NSURL *url = [NSURL URLWithString:@"http://urlgoeshere.com"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
 [request setDelegate:self];
 [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request {
 //Grab the response
 NSString *responseString = [request responseString];
        //Put the result into the ProductClass item
 item.titleOfProduct = responseString;

    //This line shows that self.tableview does NOT have an address of 0x0
 NSLog(@"%@\n", self.tableView); 

    //Problem line!!!!!! 
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    //The below lines also do nothing!!!!
 //[self.tableView reloadData];
 //[[self tableView] reloadData];
}

Anyone have any ideas??? I am at a total loss.

Cheers, Brett

A: 

You don't need the performSelectorOnMainThread - asihttprequest always calls the selectors on the main thread.

Have you setup a datasource for the table? You need to setup at least some of the fields datasource, see:

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewDatasource_Protocol/Reference/Reference.html

At least cellForRowAtIndexPath and numberOfRowsInSection. There's a tutorial here that may help:

http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/

JosephH