title: Asynchronous download issue with UITableView & ASIHTTP
body: <p>I try to load some image on my UITableView with ASIHTTP but I have some problems. First of all I read an xml file (with tbxml) and I save title, image path and description in a dictionary and then in an array, for parsing I use this code: </p>
  
  <pre><code>- (void)loadUnknownXML {    
      // Load and parse the books.xml file
      tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.xxx.com"]];
  
      // If TBXML found a root node, process element and iterate all children
      if (tbxml.rootXMLElement){
          [self traverseElement:tbxml.rootXMLElement];
      }
  }
  
  - (void) traverseElement:(TBXMLElement *)element {
  
      TBXMLElement *child = element-&gt;firstChild;
      TBXMLElement *items = [TBXML childElementNamed:@"item" parentElement:child];
  
      do{
          if (items-&gt;firstChild) {
  
              TBXMLElement *titolo = [TBXML childElementNamed:@"title" parentElement:items];
              TBXMLElement *descrizione = [TBXML childElementNamed:@"description" parentElement:items];
              //NSLog(@"Titolo: %@ \n Descrizione: %@",[TBXML textForElement:titolo],[TBXML textForElement:descrizione]);
  
              self.elemento = [[NSMutableDictionary alloc] init];
              [self.elemento setObject:[TBXML textForElement:titolo] forKey:@"Titolo"];
  
              NSString *indirizzoImmagine = [TBXML textForElement:descrizione];
              NSRange rangeSRC = [indirizzoImmagine rangeOfString:@"src=\""];
              indirizzoImmagine = [indirizzoImmagine substringFromIndex:NSMaxRange(rangeSRC)];
              NSRange rangeAMP = [indirizzoImmagine rangeOfString:@"&amp;amp"];
              NSRange rangeWidth = [indirizzoImmagine rangeOfString:@"&amp;width"];
              if (rangeAMP.location != NSNotFound) {
                  indirizzoImmagine = [indirizzoImmagine substringToIndex:NSMaxRange(rangeAMP)];
              }
              else if (rangeWidth.location != NSNotFound){
                  indirizzoImmagine = [indirizzoImmagine substringToIndex:NSMaxRange(rangeWidth)];
              }        
              indirizzoImmagine = [indirizzoImmagine stringByReplacingOccurrencesOfString:@"&amp;amp" withString:@""];
              indirizzoImmagine = [indirizzoImmagine stringByReplacingOccurrencesOfString:@"&amp;width" withString:@""];
  
  
  
              [self.elemento setObject:indirizzoImmagine forKey:@"IndirizzoImmagine"];
  
              [self.elemento setObject:[TBXML textForElement:descrizione] forKey:@"Descrizione"];
              [self.array addObject:self.elemento];
          }
  
      }
      while ((items=items-&gt;nextSibling));
  
  }
  </code></pre>
  
  <p>then I start with download</p>
  
  <pre><code>- (void) loadURL:(NSURL *)url index:(int)index
  {
  
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  
  
      ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
  
      [request setDownloadCache:cache];
      [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
      [request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
  
      [cache setStoragePath:@"/Users/kikko/kikko/xxx"];
  
      request.userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 
                          [NSNumber numberWithInt:index], @"index",
                          url, @"url", nil];
  
  
      [request setDelegate:self];
      [request startAsynchronous];
  
  }
  
  
      - (void)requestFinished:(ASIHTTPRequest *)request
      {
  
          int index = [[request.userInfo valueForKey:@"index"] intValue];
  
      ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];  
      [cache setStoragePath:@"/Users/kikko/kikko/xxx"];
  
      [request.userInfo valueForKey:@"url"];
  
      if ([cache cachedResponseDataForURL:[request.userInfo valueForKey:@"url"]]==nil) {
  
          NSLog(@"%@",[request.userInfo valueForKey:@"url"]);
  
          NSIndexPath* indexPath = [NSIndexPath indexPathForRow:index inSection:0];
          NSArray* rows = [NSArray arrayWithObjects:indexPath, nil];
          [table reloadRowsAtIndexPaths:rows withRowAnimation:UITableViewRowAnimationNone];
  
      }
  
      }
  
      - (void)requestFailed:(ASIHTTPRequest *)request
      {
          NSError *error = [request error];
          NSLog(@"Error: %@",error);
  
      }
  </code></pre>
  
  <p>at the end I put the image inside cell.imageview</p>
  
  <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  
      return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return self.array.count;
  }
  
  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      return 50;
  }
  
  -(NSString *) stringByStrippingHTML:(NSString *)stringa {
  
      NSRange r;
  
      NSString *str = stringa;
      while ((r = [str rangeOfString:@"&lt;[^&gt;]+&gt;" options:NSRegularExpressionSearch]).location != NSNotFound)
          str = [str stringByReplacingCharactersInRange:r withString:@""];
      return str; 
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"ItemCell";
  
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (!cell) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  }
  
  elemento = [array objectAtIndex:indexPath.row];
  
  NSURL *indirizzoImmagine = [NSURL URLWithString:[elemento objectForKey:@"IndirizzoImmagine"]];
  
  [self loadURL:indirizzoImmagine index:indexPath.row];
  
  ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
  [cache setStoragePath:@"/Users/kikko/kikko/xxx"];
  
  dataImmagine = [cache cachedResponseDataForURL:indirizzoImmagine];
  
  [cell.imageView setImage:[UIImage imageWithData:dataImmagine]];
  
  cell.textLabel.text = [elemento objectForKey:@"Titolo"];
  
  return cell;
  </code></pre>
  
  <p>}</p>