PDA

View Full Version : ASIHttpRequest doesn't display record in asyncronus way in tableview




AbhishekApple
Aug 18, 2010, 05:47 AM
TableView doesn't display values with asynchronous request

- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://xyz.com/abc.jsp"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];

//suppose returned string is a,b,v,d,e

NSArray *arry = nil;
arry = [responseString componentsSeparatedByString:@","];

tblProduct =[[NSMutableArray alloc] initWithArray:arry];

[responseString release];
}



- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...
NSString *cellValue = [tblProduct objectAtIndex:indexPath.row];
cell.text = cellValue;

return cell;
}


It works with Synchronus request i.e Tableview gets filled by value

- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:@"http://xyz.com/abc"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
// [request startAsynchronous] doesn't work
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSArray *arry = nil;
arry = [responseString componentsSeparatedByString:@","];

tblProduct =[[NSMutableArray alloc] initWithArray:arry];
}
}


Please help



robbieduncan
Aug 18, 2010, 05:51 AM
Have you told your table to reload the data anywhere?

AbhishekApple
Aug 19, 2010, 12:47 AM
Have you told your table to reload the data anywhere?

Thanks it worked.

But i had to create another
@interface View3Controller : UIViewController {
NSMutableArray *arryProduct;
IBOutlet UITableView *tblvProd;
}

-(void)viewDidLoad{
[tblvProd reloadData];
}


Initially i had
@interface Product : UITableViewController {
NSMutableArray *tblProduct;
}
My question is , How to call a reload method with abv interface? "I have no instance of TableView".

robbieduncan
Aug 19, 2010, 03:48 AM
"I have no instance of TableView".

It's up to you to figure out a way to have one (or notify the controller of the table view using NSNotification).