Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7058459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:06:55+00:00 2026-05-28T04:06:55+00:00

So, here’s the code for my cellForRowAtIndexPath: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  • 0

So, here’s the code for my cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]autorelease];
    }

    NSInteger artistIndex = 1; // 1
    NSInteger albumIndex = 3;  // 3
    NSInteger dateIndex = 6;   // 6
    NSInteger imageIndex = 8;  // 5

    // ARTIST
    CGRect frame = CGRectMake(59, 11, 244, 13);
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.font = [UIFont boldSystemFontOfSize:13];
    label.textColor = [UIColor blackColor];
    label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:artistIndex];
    [cell addSubview:label];

    // ALBUM (more like description...
    frame = CGRectMake(60, 30, 244, 11);
    label = [[UILabel alloc]initWithFrame:frame];
    label.font = [UIFont boldSystemFontOfSize:11];
    label.textColor = [UIColor darkGrayColor];
    label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:albumIndex];
    [cell addSubview:label];

    // DATE
    frame = CGRectMake(59, 49, 244, 10);
    label = [[UILabel alloc]initWithFrame:frame];
    label.font = [UIFont fontWithName:@"Helvetica" size:10.0];
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = UITextAlignmentRight;
    label.text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:dateIndex];
    [cell addSubview:label];

    // IMAGE
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:imageIndex];
    imageView.frame = CGRectMake(8,9,44,44);
    [imageView.layer setMasksToBounds:YES];
    [imageView.layer setCornerRadius:3.0];
    [[cell contentView] addSubview:imageView];

    [imageView release];
    [label release]; 

    return cell;
}

To explain what’s going on, basically it just taps into an array that stores strings for everything except for index 8 (which stores a UIImage).

I have no variables set to autorelease (mostly because I don’t really understand autorelease just yet lol)

Anyways, when scrolling through the app, it will slow down progressively (mostly because of the UIImage, but also in part because of the labels and frames).

Is there anyway to manage my memory better? Also, is there a nicer way of coding this? I was thinking of making an array of UITableViewCells and just accessing those via cellForRowAtIndexPath.

So I would love for some suggestions, thanks guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:06:55+00:00Added an answer on May 28, 2026 at 4:06 am

    You’re leaking a lot of labels there. Every time you alloc/init a new label you need to release it. Currently you are re-using the same label variable several times by assigning it a new label object without releasing the old one.

    Instead of putting your [label release] at the end, put [label release] after every [cell addSubview:label];

    Your next problem is you’ve misunderstood how the table cell recycling works. UITableCell objects are reused over and over again within a table, that why you do this bit:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]autorelease];
    }
    

    That’s basically saying “check if there’s a cell available, and if not create a new one”. When a cell is re-used it keeps all the old stuff you’ve added to it. So when you add those labels and image view to the cell, they stay in that cell the next time it is used, and the next time.

    But because you add the labels again each time the cell is re-used, it builds up multiple copies of the labels, so the second time the cell is displayed it has twice as many labels, and the next time 3 times as many, and so on. You can’t see the duplicates because they’re in exactly the same place, but they’re there using up memory and slowing your table down.

    You need to move the code where you append new views to the cell inside the “if (cell == nil) { … }” statement, so the labels are only added once when the cell is created.

    Of course that means that every cell will have the same text and image, which you don’t want, so you need to split out the logic where you set the text and images and put it after the if statement. That way you only create the labels once but you set the text each time they are displayed in a different table row. That’s a bit tricky because you don’t have the references to the labels any more, but you can do it by setting unique tags on the labels so you can find them again.

    Here’s a cleaned-up version with all the leaks removed:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    
        NSInteger artistTag = 1;
        NSInteger albumTag = 2;
        NSInteger dateTag = 3;
        NSInteger imageTag = 4;
    
        if (cell == nil) {
    
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"]autorelease];
    
            // ARTIST
            CGRect frame = CGRectMake(59, 11, 244, 13);
            UILabel *label = [[UILabel alloc]initWithFrame:frame];
            label.font = [UIFont boldSystemFontOfSize:13];
            label.textColor = [UIColor blackColor];
            label.tag = artistTag;
            [cell addSubview:label];
            [label release];
    
            // ALBUM (more like description...
            frame = CGRectMake(60, 30, 244, 11);
            label = [[UILabel alloc]initWithFrame:frame];
            label.font = [UIFont boldSystemFontOfSize:11];
            label.textColor = [UIColor darkGrayColor];
            label.tag = albumTag;
            [cell addSubview:label];
            [label release];
    
            // DATE
            frame = CGRectMake(59, 49, 244, 10);
            label = [[UILabel alloc]initWithFrame:frame];
            label.font = [UIFont fontWithName:@"Helvetica" size:10.0];
            label.textColor = [UIColor darkGrayColor];
            label.textAlignment = UITextAlignmentRight;
            label.tag = dateTag;
            [cell addSubview:label];
            [label release];
    
            // IMAGE
            UIImageView *imageView = [[UIImageView alloc]init];
            imageView.frame = CGRectMake(8,9,44,44);
            imageView.tag = imageTag;
            [imageView.layer setMasksToBounds:YES];
            [imageView.layer setCornerRadius:3.0];
            [[cell contentView] addSubview:imageView];
            [imageView release];
        }
    
        NSInteger artistIndex = 1; // 1
        NSInteger albumIndex = 3;  // 3
        NSInteger dateIndex = 6;   // 6
        NSInteger imageIndex = 8;  // 5
    
        ((UILabel *)[cell viewWithTag:artistTag]).text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:artistIndex];
    
        ((UILabel *)[cell viewWithTag:albumTag]).text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:albumIndex];
    
        ((UILabel *)[cell viewWithTag:dateTag]).text = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:dateIndex];
    
        ((UIImageView *)[cell viewWithTag:imageTag]).image = [[musicList.list objectAtIndex:indexPath.row] objectAtIndex:imageIndex];
    
    
        return cell;
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code, which takes two version identifiers in the form 1, 5,
Here is an example: I write html code inside of textarea, then I swap
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that
here are 2 screen shots when i try to debug my code in visual
Here a simple question : What do you think of code which use try
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here is some code on the javascript side for form-based uploads: iframe.setAttribute('src', 'javascript:false;'); I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.