Question

I'm having some trouble with lag in my UITableview. There aren't any problems on an iPhone 5 and after I started caching images in an NSDictionary, the iPhone 4 with iOS 6 became very responsive. The problem remains on an iPhone 4 with iOS 7 however.

I've read trough some threads here with tips about making views opaque which I did but it didn't help. All my views except the labels are opaque (because if they are opaque they fill and that won't work for my purpose)

I do load a background image from the storyboard, do you guys know if this might be affecting performance? Is the storyboard inefficient when it comes to loading images?

Do you have any other tips for improving performance on a UITableView?

Thanks in advance!

Some code as requested,and these are the elements on the cell: http://imgur.com/Dcif6QE

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

if([self.eventList lastObject])
{
    static NSString *CellIdentifier = @"HomeListCell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.userInteractionEnabled = YES;
    cell.event = [self.eventList objectAtIndex:indexPath.row];
    cell.parent = self;

    if([cell.event.event.event_type count] != 0)
    {
        Event_Type *eventType = [cell.event.event.event_type firstObject];
        NSString *imageName = @"HomeList_Type";
        imageName = [imageName stringByAppendingString:eventType.name];
        cell.eventTypeImage.image = [self.imageDict objectForKey:imageName];
    }

    //Laad de images hier uit de cache om scroll performance te verbeteren
    int score = [cell.event.rating intValue];
    [cell moveView:cell.ratingNumber duration:0.0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
    if(score > 0)
    {
        cell.ratingImage.image = [self.imageDict objectForKey:@"HomeList_plus"];
    }
    else if(score == 0)
    {
        cell.ratingImage.image = nil;
        [cell moveView:cell.ratingNumber duration:0.0 curve:UIViewAnimationCurveLinear x:0.0 y:-10.0];
    }
    else
    {
        cell.ratingImage.image = [self.imageDict objectForKey:@"HomeList_min.png"];
        score = -score;
    }
    cell.ratingNumber.text = [NSString stringWithFormat:@"%d", score];

    [cell styleSelf];
}

And styleSelf has this code:

-(void) styleSelf {
LocationManager *locationManager = [LocationManager sharedInstance];

//Tekens die verandert moeten worden
NSCharacterSet *notAllowedY = [NSCharacterSet characterSetWithCharactersInString:@"ÿ"];
NSString *resultString = [[event.event.name componentsSeparatedByCharactersInSet:notAllowedY] componentsJoinedByString:@"y"];

//Afstand berekening
double eventLong = [self.event.location.address.gps_long doubleValue];
double eventLat = [self.event.location.address.gps_lat doubleValue];
CLLocation* locatie = [[CLLocation alloc]initWithLatitude:eventLat longitude:eventLong];

//Date + time
NSString *eventDate = event.opening;
eventDate = [eventDate stringByReplacingOccurrencesOfString:@"T" withString:@" "];
NSDate *theDate;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

if([eventDate hasSuffix:@"Z"])
{
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ssZ"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    theDate = [dateFormatter dateFromString:eventDate];
}
else
{
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    theDate = [dateFormatter dateFromString:eventDate];
}
[dateFormatter setDateFormat:@"HH:mm"];
self.timeNumberLabel.text = [dateFormatter stringFromDate:theDate];
self.timeNumberAfscheurLabel.text = [dateFormatter stringFromDate:theDate];

[dateFormatter setDateFormat:@"MM-dd"];
if ([[dateFormatter stringFromDate:theDate] isEqualToString:[dateFormatter stringFromDate:[NSDate date]]])
{
    self.timeWhenLabel.text = NSLocalizedString(@"HomeList-Vandaag", nil);
    self.timeWhenAfscheurLabel.text = NSLocalizedString(@"HomeList-Vandaag", nil);
}
else
{
    [dateFormatter setDateFormat:@"MM"];
    NSString *maand = [dateFormatter stringFromDate:theDate];
    NSString *monthName = NSLocalizedString([@"Maand-" stringByAppendingString: maand], nil);
    [dateFormatter setDateFormat:@"d"];
    NSString *dag = [dateFormatter stringFromDate:theDate];

    NSString *DatumString = [[dag stringByAppendingString:@" "]stringByAppendingString:monthName];

    self.timeWhenLabel.text = [@"  " stringByAppendingString:DatumString];
    self.timeWhenAfscheurLabel.text = [@"  " stringByAppendingString:DatumString];
}

//De cell vormen of de user gaat of niet
if([event.user_attends_event count] == 0)
{
    [self moveView:self.nietAfgescheurdKnop duration:0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
    [self moveView:self.timeNumberAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
    [self moveView:self.timeWhenAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:0.0 y:0.0];
}
else
{
    [self moveView:self.nietAfgescheurdKnop duration:0 curve:UIViewAnimationCurveLinear x:50.0 y:0.0];
    [self moveView:self.timeNumberAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:50.0 y:0.0];
    [self moveView:self.timeWhenAfscheurLabel duration:0 curve:UIViewAnimationCurveLinear x:50.0 y:0.0];
}

self.event.userDistance = [locationManager getDistanceBetween:locatie];
if([self.event.userDistance isEqualToString:@"GPS error"])
{
    self.distanceNumberLabel.text = NSLocalizedString(@"Extras-GPS", nil);
    self.distanceTypeLabel.text = NSLocalizedString(@"Extras-UIT", nil);
    self.distanceNumberLabel.textColor = [UIColor grayColor];
    self.distanceTypeLabel.textColor = [UIColor grayColor];
}
else
{
    NSString *placehold = self.event.userDistance;
    placehold = [placehold stringByReplacingOccurrencesOfString:@"." withString:@","];
    self.distanceNumberLabel.text = placehold;
    self.distanceTypeLabel.text = NSLocalizedString(@"Extras-Km", nil);
    self.distanceNumberLabel.textColor = [UIColor blackColor];
    self.distanceTypeLabel.textColor = [UIColor blackColor];
}

// Configure the cell...
self.titleLabel.text = resultString;
self.tagsLabel.text = [event getMetadataString];

}

Was it helpful?

Solution

Your evil culprit is NSDateFormatter. This is a super-heavy object to create. You should create a single version of it somewhere and reuse it, setting the properties (formats, time zones, etc.) freely.

It's also a good idea to use Instruments -> Time Profiler to see exactly which methods are taking up time on the main thread.

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