UITableView scrolling slow, memory leak issue

I have a UITableView and a UITableCell subclass. Each table cell has two scrollviews that each rotate a dynamic amount of labels, which are created in my table cell implementation. I have poor scrolling performance and I believe it is from memory leaks. I’m referencing this stackoverflow correct answer to fix my problem: cellForRowAtIndexPath memory management.

I can’t figure out how to tweak my code so I don’t have to allocate memory every time I create labels.

  • Where do you file bugs for Android/iOS Google Products or Services (AppInvites, Maps, etc…)?
  • How to convert an Int into NSData in Swift?
  • Is there a way to access the currently played track while the iPhone is connected to an accessory?
  • UIAlertController custom font, size, color
  • Library for both iOS and OS X apps?
  • to drawRect or not to drawRect (when should one use drawRect/Core Graphics vs subviews/images and why?)
  • ViewController.m

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Custom Cell";
    
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    
    NSDictionary *dictionary = [parseDataArray objectAtIndex: indexPath.row];
    
    NSArray *popularLinkTitleArray = [dictionary objectForKey:@"popularLinkTitleArray"];
    NSString *soundCloudLink = [dictionary objectForKey:@"soundCloudLink"];
    NSArray *soundCloudTrackTitleArray = [dictionary objectForKey:@"soundCloudTrackTitleArray"];
    
    
    cell.artistNameLabel.text = artistName;
    
    [cell createPopularLinkLabels: popularLinkTitleArray];
    
    return cell;
    }
    

    CustomCell.m

    - (void)layoutScrollLabelsForPopularLinkScrollView: (float)arrayCount
    {
    UIView *view = nil;
    NSArray *subviews = [popularLinkScrollView subviews];
    
    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UILabel class]] && view.tag >= 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;
    
            curXLoc += (kScrollObjWidth);
        }
    }
    
    [popularLinkScrollView setContentSize:CGSizeMake((arrayCount * kScrollObjWidth),    [popularLinkScrollView bounds].size.height)];
    
    }
    
    -(void) createPopularLinkLabels:(NSArray *) popularLinkTitleArray
    {
    
    popularLinkScrollView.clipsToBounds = YES;
    
    kScrollObjHeight = popularLinkScrollView.frame.size.height;
    kScrollObjWidth = popularLinkScrollView.frame.size.width;
    
    for (UIView* subView in popularLinkScrollView.subviews)
        [subView removeFromSuperview];
    
    NSUInteger i;
        for (i = 0; i < popularLinkTitleArray.count; i++)
        {
            NSString *string = [NSString stringWithFormat:@"%@", [popularLinkTitleArray objectAtIndex: i]];
            UILabel *label = [[UILabel alloc] init];
            label.text = [NSString stringWithFormat:@"%@", string];
            label.backgroundColor = [UIColor clearColor];
            label.numberOfLines = 5;
            [label setFont:[UIFont fontWithName:@"Calibri" size:18]];
    
            // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
            CGRect rect = label.frame;
            rect.size.height = kScrollObjHeight;
            rect.size.width = kScrollObjWidth;
            label.frame = rect;
            label.tag = i;  // tag our images for later use when we place them in serial fashion
            [popularLinkScrollView addSubview:label];
        }
    
    [self layoutScrollLabelsForPopularLinkScrollView:popularLinkTitleArray.count];
    
    }
    

    2 Solutions Collect From Internet About “UITableView scrolling slow, memory leak issue”

    The first thing you could do is try to reuse UILabels in createPopularLinkLabels. Right now, you are just removing them before adding more.

    But if the number of popular links is not the same in all the cells, then you will have to remove the remaining ones or hide them and there will be no way to avoid memory allocations.

    A better solution would be to insert a custom UIView inside your scroll view and draw all the texts manually instead of creating tons of subviews.

    Hope this helps,
    Thomas

    Are you using ARC? If not, you should autorelease to

    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]
    

    Cell objects are reused and you do not need to re-add subviews each time you change the contents. Just change the data and update the label frames if needed.

    If you have distinctly different cells, consider using separate custom cell classes. You will need to use a separate value for the reuseIdentifier.

    Also consider not creating a custom cell but just adding elements to the standard UITableViewCell. Read this about styling a cell.