Search code examples
iphoneobjective-cipaduitableviewuisegmentedcontrol

Debugging app artwork slow down in Objective-C


I have a tableviewcontroller, specifically, in my app which is getting a little jittery when scrolling.

I fairly certain it's because of my custom UISegmentedControl appearance, I have four in my table, each in their own cell.

I use this code in my app delegate to customise them:

[[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:@"SegmentedControl.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 4)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:@"SegmentedControlSelected.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 4)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"SegmentedControlDivider.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"SegmentedControlSelectedDivider.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"SegmentedControlSelectedDivider.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                             [UIColor whiteColor], UITextAttributeTextColor,
                                                             [UIColor grayColor], UITextAttributeTextShadowColor,
                                                             [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
                                                             [UIFont fontWithName:@"Georgia-Italic" size:0.0], UITextAttributeFont,
                                                             nil] forState:UIControlStateNormal];

Why might this cause such a slow down? What could I do to improve performance? It jitters as they come onto the screen. I have never had a problem like this before.

I'm fairly sure its the switches and segmented controls, as when I remove them it speeds up and goes all silky smooth. I basically do this:

if (indexPath.row == 0)
        {
            cell.textLabel.text = @"text";
            UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"1", @"2", nil]];
            [segControl setSelectedSegmentIndex:[myBool boolValue]];
            [segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
            [segControl addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = segControl;
        }

In my cellForRowAtIndexPath 7 times, how can I make this faster?


Solution

  • I have an app that I am working on and I had some similar customizations. I recommend reusing the set controls... You can accomplish this by making a UITableViewCell subclass and setting up your seg control in it, then you can create outlets for the things you need to customize.

    A quick search found this tutorial and it looks like what I am talking about

    UITableViewCell Prototype Tutorial

    This way your cells will be completely reusable and therefore, much faster.

    But then, I don't now your exact use case, so maybe this won't apply to you. It sped up my cells with custom buttons, art, and animations quite a bit.

    Just remember alloc inits are very slow.

    I guess another option would be to pre-init all that you can with the controls and maybe store them in and array then only load them in cellForRowAtIndexPath: