How can I speed up a UITableView?

前端 未结 9 1868
广开言路
广开言路 2020-12-22 23:17

I have a UITableView with about 400 cells in 200 sections and it\'s a little sluggish in responding to user interaction (scrolling, selecting cells.) I\'ve made sure the met

相关标签:
9条回答
  • 2020-12-23 00:01

    Two suggestions: One is to use -initWithStyle:reuseIdentifier: for your table view cells instead of -initWithFrame:. The other is to comment out setting the cell.backgroundView to an image with a gradient and see if that's the culprit. Every time I've had poor performance in a table view it's been because of an image.

    0 讨论(0)
  • 2020-12-23 00:02
    1. use a shared image instance for the background (you alloc/init/release one for every time a new cell is created). When your table view is big , this means that the background X cells in memory takes much more memory than it should.

      instead of
      cell.backgroundView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell gradient2.png"]];
      just use :
      cell.backgroundView= [SomeHelperClass sharedBackgroundUIImageResource];

    2. If that doesn't help , use CG instead of labels and other subviews (a screenshot will help here.. to know what we're talking about).

    0 讨论(0)
  • 2020-12-23 00:02

    This is a little off-topic here (because you have only one background image for all cells):

    My app displays different images in each cell. After adding about 5 cells to UITableView - table is slowed down drastically. It takes about 1-2 seconds to process all images each time i open view controller.

    if let image = UIImage(contentsOfFile: photoFile){
        // just set it and let system fit it
        //cell.imageView!.image = image
    
        // 1 - Calculate sized
        let DEFAULT_THUMBNAIL_WIDTH: CGFloat  = (cellHeight / 4) * 5;
        let DEFAULT_THUMBNAIL_HEIGHT: CGFloat = cellHeight;
    
        let aspectRatio: CGFloat = image.size.width / image.size.height
        var willBeHeight = DEFAULT_THUMBNAIL_HEIGHT
        var willBeWidth  = DEFAULT_THUMBNAIL_HEIGHT * aspectRatio
    
        if(willBeWidth > DEFAULT_THUMBNAIL_WIDTH){
            willBeWidth = DEFAULT_THUMBNAIL_WIDTH
            willBeHeight = willBeWidth / aspectRatio
        }
    
        let eps:CGFloat = 0.000001
        assert((willBeHeight - eps) <= DEFAULT_THUMBNAIL_HEIGHT);
        assert((willBeWidth - eps) <= DEFAULT_THUMBNAIL_WIDTH);
    
        // 2 - Create context
        var size:CGSize = CGSize(
            width: DEFAULT_THUMBNAIL_WIDTH,
            height: DEFAULT_THUMBNAIL_HEIGHT)
        UIGraphicsBeginImageContext(size)
    
        // one-to-one rect
        //var imageRect: CGRect = CGRectMake(0.0, 0.0, size.width, size.height)
        var imageRect: CGRect = CGRectMake(0.0, 0.0, willBeWidth, willBeHeight)
    
        // 3 - Draw image
        image.drawInRect(imageRect)
        var imageResult: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        cell.imageView!.image = imageResult
    
        UIGraphicsEndImageContext()
    }else{
        DDLogError("Can not draw photo: \(photoFile)")
    }
    

    So i've ended up with generating small THUMBNAILS for all my images.

    0 讨论(0)
提交回复
热议问题