UITableview with images scrolls very slowly

I have a UITableView that loads images from its table images from a server.

I noticed that the table scrolls very slowly.

I thought this might be due to the download, but I realized that the table still scrolls slowly after the download is complete, and the image icon size is very smaller.

Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { btnBack.hidden = FALSE; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; ///////////////////// Cell other accessories ///////////////////// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.backgroundColor = [UIColor clearColor]; // cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"list_1.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]]; // cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"list_2.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]]; ///////////////////// Cell Title ///////////////////// cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:17.0]; cell.textLabel.font = [UIFont boldSystemFontOfSize:17.0]; cell.textLabel.textColor = [UIColor blackColor]; cell.textLabel.highlightedTextColor = [UIColor blackColor]; } ///////////////////// Cell Title ///////////////////// cell.textLabel.text = [NSString stringWithFormat:@" %@", [test.arrTitle objectAtIndex:indexPath.row]]; ///////////////////// Cell Image ///////////////////// NSString *Path; Path = [NSString stringWithFormat:@"http://%@",[test.arrImages objectAtIndex:indexPath.row]]; NSLog(@"image-->%@",[test.arrImages objectAtIndex:indexPath.row]); NSString *strImage = Path; NSURL *url4Image = [NSURL URLWithString:strImage]; NSData *data = [NSData dataWithContentsOfURL:url4Image]; image =[[UIImage alloc] initWithData:data]; cell.imageView.image =image; [image release]; return cell; } 
+4
source share
6 answers

You should use NSOperationQueue to handle lazy loading of images and the user table tableviewcell.
Google for tweetie custom tableviewcell This should put you in the right direction.

Apple has a sample project for loading images into tableViews: LazyTableImages

+8
source

Perhaps your image list_bg_01.png large? This method takes up a lot of memory, you should use it only when the patternimage is really small and simple, in other conditions it is better to use a UIImageView with an image as a background view.

+1
source

Did you run the profiler on your code? As mentioned in Nik, you must download images from the network asynchronously using GCD or NSOperation. You can watch the Harvard iTunesU video for NSOperation: this will give you a clear example with good explanations.

And you must definitely run the profiler to find out where you spend the most time. A cell with multiple images should load quickly.

Please note that some operations with the cell that can be performed during initialization (for example, setting the background color in the cell) can lead to high costs if you do them again.

If you have a special layout for your cell, you must subclass UITableViewCell and implement the layoutSubviews method (remember to call super). Doing a lot of work in the ViewDataSourceDelegate table (and forcing relay redirection) can really be inefficient.

But then again: run the profiler

+1
source

I had the same problem and it was fixed if you did not use the cornerRadius property. Then we used a background image with a cell shape.

+1
source

I wonder why you create your image inside the viewcell code. Can't you create it once for the whole class and then put it in a UIView on request?

0
source

The configuration of the overall general appearance should be in if (cell == nil) {} Only the establishment of unique materials, such as name, etc., Must be outside. Also, if your cell background is simple (like a gradient), use QuartzCore to draw it or use CAGradientLayer as a sublayer for the contentView layer.

0
source

Source: https://habr.com/ru/post/1387735/


All Articles