views:

64

answers:

1

I have a UITableView with custom cells that were defined in the xib file, and am experiencing poor scrolling performance (choppy) on my device when the cells have a UISegmentedControl on them. NSLog statements reveal that the cells are being allocated and reused as they ought. My code for cellForRowAtIndexPath method is below. Connections are made in the xib as per Apple's documentation. (Scrolls smoothly in simulator btw)

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

    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell =  
           [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) 
    {
        [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" 
                               owner:self  
                               options:nil];  
        cell = self.tvCell;
        self.tvCell = nil;
    }

    cell.layer.shouldRasterize = YES;     // build error is here

    UILabel *lbl = (UILabel *)[cell viewWithTag:1];

    [lbl setText:[NSString stringWithFormat:@"Q%i", indexPath.row+1]];  

    return cell;
}
A: 

Any drawing that a table cell has to do while it's being scrolled is going to cause performance issues; when you have a lot of subviews, there tends to be a lot of drawing going on, and that will—as you've observed—make your scrolling pretty choppy. There are a couple of ways to try to reduce that.

The first step is to make sure that your cells themselves, and as many of their subviews as possible, have their opaque properties set to YES. Opaque views don't have to get blended with the content underneath them, and that saves a lot of time.

You may also want to set your cells' layers to rasterize themselves, like this:

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

This will collapse your view hierarchy into one flat bitmap, which is the kind of thing Core Animation just loves to draw. Note that any animating views—activity indicators, for instance—will force that bitmap to be updated every time they change, i.e. a lot. In that case, you won't want the cell to rasterize everything; you might just use a subview with all of your relatively static views (e.g. labels) beneath another subview with any such dynamic content, and only have the first of those rasterized.

Noah Witherspoon
Noah, after adding the first line of code you suggested, and then also adding the QuartzCore framework so I can refer to the layer, I get build error: "request for member 'shouldRasterize' in something not a structure or union". Would you please say more about what is required to get those two lines of code working?
Alyoshak
That error might come up if you don't have #import <QuartzCore/QuartzCore.h> in your .m; also, shouldRasterize is only available in iOS 3.2 and later.
Noah Witherspoon
Yes is imported in my .m file, and <QuartzCore/CALayer.h> also. But, nope, not using 3.2 (using 3.1.3). Must be the problem. Yeah, and I can't even build successfully targeting this device (iPodTouch) with iOS 3.2. Code signing error and another weird one regarding iOS 3.2. Both are here: [BEROR]CodeSign error: code signing is required for product type 'Application' in SDK 'Device - iPhone OS 3.2'[BWARN]warning: building with 'Targeted Device Family' set to iPhone only ('1') not supported with SDK 'Device - iPhone OS 3.2'.
Alyoshak
So, is there a way to get rid of the choppiness of the scrolling without going to 3.2? 3.2 is just for the iPad anyway, right? When I used it in simulator mode an iPad version of the simulator came up. I need to address the scrolling issue on iPodTouch devices.
Alyoshak
Any app that you submit to the App Store today needs to be built with the 4.0 SDK. If you set your target's "Deployment Target" to 3.1.x, it will still run on older devices while allowing you to use features of the newer SDK, like this one; in that case, you need to check at runtime (using -respondsToSelector:) whether CALayer implements the setShouldRasterize method.
Noah Witherspoon
Noah, I intend to test this and check off your answer as the correct one. Haven't been able to get back to this.
Alyoshak