CoreCode
CoreCode

Reputation: 2227

Poor scrolling performance in UITableView with cell subviews.

I have a UITableView with many rows. Each row has a UIView in it that I edit in the cellForRowAtIndexPath. My problem is that this lags terribly on an iPod touch. I am not sure why this is happening as the iPod should be able to take more than 11 UIViews on the screen at once? Does anyone know why this is happening?

EDIT - here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIView *colorView = (UIView *)[cell viewWithTag:1];
    UILabel *label = (UILabel *)[cell viewWithTag:2];
    colorView.layer.cornerRadius = 10;
    colorView.layer.borderColor = [UIColor blackColor].CGColor;
    colorView.layer.borderWidth = 2.0f;
    colorView.layer.masksToBounds = YES;
    switch (indexPath.row) {
        case 0:
            label.text = @"White";
            colorView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
            break;
        case 1:
            label.text = @"Olive";
            colorView.backgroundColor = [UIColor colorWithRed:0.814 green:0.551 blue:0.119 alpha:1];
            break;
        case 2:
            label.text = @"Dark Blue";
            colorView.backgroundColor = [UIColor colorWithRed:0.036 green:0 blue:1 alpha:1];
            break;
        case 3:
            label.text = @"Dark Green";
            colorView.backgroundColor = [UIColor colorWithRed:0 green:0.387 blue:0.006 alpha:1];
            break; 
        case 4:
            label.text = @"Orange";
            colorView.backgroundColor = [UIColor colorWithRed:1 green:0.500 blue:0 alpha:1];
            break; 
        case 5:
            label.text = @"Dark Brown";
            colorView.backgroundColor = [UIColor colorWithRed:0.399 green:0.269 blue:0.137 alpha:1];
            break;
        case 6:
            label.text = @"Dark Red";
            colorView.backgroundColor = [UIColor colorWithRed:0.530 green:0.017 blue:0 alpha:1];
            break; 
        case 7:
            label.text = @"Maroon";
            colorView.backgroundColor = [UIColor colorWithRed:0.502 green:0 blue:0.251 alpha:1];
            break; 
        case 8:
            label.text = @"Yellow";
            colorView.backgroundColor = [UIColor colorWithRed:0.865 green:0.864 blue:0.002 alpha:1];
            break; 
        case 9:
            label.text = @"Purple";
            colorView.backgroundColor = [UIColor colorWithRed:0.460 green:0 blue:0.865 alpha:1];
            break; 
        default:
            label.text = @"";
            colorView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
            break;
    }
    return cell;
}

Thanks

Upvotes: 0

Views: 1262

Answers (1)

jrturton
jrturton

Reputation: 119242

There are a few things you can do here.

Turn on quartz debugging (on the device using instruments or, as of quite recently, in the simulator). Colour blended layers - you should see mostly green. If you don't, then make sure your new views are marked as opaque. Scrolling transparent views takes a lot of performance.

If that doesn't help, profile using the time profiler in instruments. Where is the time being spent? Should you, for example, be caching the colours you are using for the background?

Your layer properties should ideally only be set once. awakeFromNib in a custom cell subclass would be a good place to do this.

Upvotes: 2

Related Questions