UITableview with images scroll very slow

2019-04-09 17:55发布

问题:

I have a UITableView which downloads its tableviewcells images from a server.

I observed that the table scroll very slowly.

I thought that this might be due to the downloading, but I have realized that the table still scroll slow after download has finished and the image icon size is very less.

The 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;
}

回答1:

You should look to use an NSOperationQueue to handle lazy loading of images and a custom tableviewcell.
Google for tweetie custom tableviewcell That should set you in the right direction.

Apple has a sample project for downloading images in tableViews: LazyTableImages



回答2:

Maybe your list_bg_01.png image is big? This method take a lot of memory, you should use it only when patternimage really small and simple, in other conditions better to use UIImageView with image as background view.



回答3:

Have you run the profiler on your code? As Nik burns mentioned you should load your images from the network asynchronously using either GCD or NSOperation. You can watch the Harvard iTunesU video for NSOperation: it will give you a clear example with good explanations.

And you should definitely run the profiler to see where you are spending the most time. A cell with a few images should load fast.

Note that some operations on the cell which could be done at initialization (such as setting the background color on a cell) can end-up costing you a lot of performance if you do them repeatedly.

Als if you have a special layout for your cell you should subclass the UITableViewCell and implement the layoutSubviews method (don't forget to call super). Doing lots of work in the tableViewDataSourceDelegate (and forcing relayout as you do) can really be inefficient.

But again: run the profiler



回答4:

I had the same issue and it was corrected by not using the cornerRadius property. Then we used a background image with the form of the cell.



回答5:

I wonder why you create your image inside the viewcell code. Can't you create it once for the whole class and then position it into the UIView on demand?



回答6:

Configuring of all general appearance should be in if (cell == nil) {} Only setting unique stuff, such as title and so on should be outside. Also, if your cell's background is simple (such as gradient), use the QuartzCore to draw it or use CAGradientLayer as sublayer to contentView's layer.