UITableview with images scroll very slow

2019-04-09 17:49发布

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

6条回答
在下西门庆
2楼-- · 2019-04-09 18:29

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.

查看更多
We Are One
3楼-- · 2019-04-09 18:35

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.

查看更多
戒情不戒烟
4楼-- · 2019-04-09 18:39

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

查看更多
【Aperson】
5楼-- · 2019-04-09 18:41

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.

查看更多
爷的心禁止访问
6楼-- · 2019-04-09 18:50

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?

查看更多
Rolldiameter
7楼-- · 2019-04-09 18:52

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

查看更多
登录 后发表回答