0
 The uitableview view gets slow while receiving images from server through Json

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

    NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
    UITableViewCellFixed *cell = (UITableViewCellFixed *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCellFixed alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                            reuseIdentifier:CellIdentifier] autorelease];;
    }

   NSURL *url = [NSURL URLWithString:[dict objectForKey:@"allfeeds3"]];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
 // cell.imageView.image = [UIImage imageWithData:data];
    UIImage *tmpImage = [[UIImage alloc] initWithData:data];
    cell.imageView.image = tmpImage;

}
Ahsan Muhammad
  • 89
  • 1
  • 12
  • This has been asked SOOOOOOOO many times here... please search before just posting your question. The answers are already here... – Mark Granoff Apr 09 '12 at 15:28
  • 1
    Check this: [Asynchronous loading of images](http://stackoverflow.com/questions/5840451/asynchronous-loading-of-images), [Loading an image into UIImage asynchronously](http://stackoverflow.com/questions/9786018/loading-an-image-into-uiimage-asynchronously) – Lucien Apr 09 '12 at 15:31

1 Answers1

1

I am sure this same question has been asked before, and most likely by you, as the code is extremely similar.

You're creating a new cell every time the datasource is asking its delegate for one. This line of code:

 NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];

results in a new cell getting created every time. If each cell is the same (in terms of layout, not content), you use a single static NSString for the reuse identifier. This way, the tableview will try to pull a reusable cell out of its queue instead of creating one each time.

In addition, it looks like you're trying to just download the data on the main thread. AND you alloc/init a new UIImageView even if the cell DOES get reused. You need to subclass UITableViewCell and either set up the UIImageView in layoutSubviews, or load it from a nib. Your whole approach to this needs to be rethought. Carefully.

jmstone617
  • 5,707
  • 2
  • 24
  • 26