ASIHttpRequest doesn't display record in asyncronus way in tableview

Discussion in 'iOS Programming' started by AbhishekApple, Aug 18, 2010.

  1. macrumors member

    Joined:
    Aug 5, 2010
    #1
    TableView doesn't display values with asynchronous request
    Code:
    - (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
    Code:
    - (void)viewDidLoad
    {
      NSURL *url = [NSURL URLWithString:@"http://xyz.com/abc"];
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
      [B][request startSynchronous];[/B] 
     // [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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Have you told your table to reload the data anywhere?
     
  3. thread starter macrumors member

    Joined:
    Aug 5, 2010
    #3
    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".
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    It's up to you to figure out a way to have one (or notify the controller of the table view using NSNotification).
     

Share This Page