Question

Most of the views in my app are UITableVlews inside a UIViewController. My App feels like it's lagging when trying to scroll through the tables. I was wondering (1.) if it is better to create the cell objects in the table view, or create them at runtime and add them to the cells subview?

examples:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            case 3: 
            {
                NSNumber *tetherState = [[mobIntDict objectForKey:@"State"] intValue];
                NSNumber *currValState = [[NSNumber numberWithInt:1023] intValue];
                tetherSw = [[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)];
                tetherSw.tag = kDefaultSwTag;
                if(tetherState == currValState){
                    tetherSw.on = YES;
                }else{
                    tetherSw.on = NO;
                }
                [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged];
                [cell.contentView addSubview:tetherSw];
                cell.textLabel.text = @"Tether";
                [tetherSw release];
            }
                break;
}

-OR-

-(void)viewDidLoad{

    tetherSw = [[[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)] autorelease];
    tetherSw.tag = kDefaultSwTag;
    [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            case 3: 
            {
                 [cell addSubView:tetherSw];
            }
}
Was it helpful?

Solution 3

Actually, my tables are setup just fine. A solid restore did the trick, and my app runs without the afore-mentioned "LAG"

OTHER TIPS

It doesn't matter. Your cell is complex and therefore your view lags.

If you want performance, avoid the UISwitch. Toggle the cell's checkmark instead. In fact, just avoid any fancy table view cell subclasses or custom backgrounds, to reduce the size of the view hierarchy.

Are you properly de-queuing and reusing cells?

It would optimise things a lot if you reused a cell, say a @"SwitchCell", that would speed up scrolling a lot. Currently a lot of time will be spent adding the switch to the cell's content view (laying out of views etc,) and performing other tasks that only need to happen once in a cells lifetime, instead of every time a new cell appears while scrolling.

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

    // Create cell
    static NSString *CellIdentifier = @"SwitchCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UISwitch *tetherSw = [[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)];
        tetherSw.tag = kDefaultSwTag;
        [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:tetherSw];
        [tetherSw release];
    }

    // Setup for each cell
    cell.textLabel.text = @"Tether";
    NSNumber *tetherState = [[mobIntDict objectForKey:@"State"] intValue];
    NSNumber *currValState = [[NSNumber numberWithInt:1023] intValue];
    UISwitch *tetherSw = (UISwitch *)[cell.contentView viewWithTag: kDefaultSwTag];
    tetherSw.on = (tetherState == currValState);

    // Return
    return cell;

}

See the docs for dequeueReusableCellWithIdentifier for more information on dequeuing.

Also, make sure that there is no transparency in any of your cell's subviews. This causes a lot of lagging. Make sure any labels or anything else you add has opaque = YES and a background color set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top