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 1036885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:46:36+00:00 2026-05-16T14:46:36+00:00

I am trying to add custom labels to my cell in UITableView. When I

  • 0

I am trying to add custom labels to my cell in UITableView. When I try to do this the display is messed up and I am unable to figure out what exactly is going on. Please find the image below and I will post the method I wrote..

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


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //cell.textLabel.font=[UIFont systemFontOfSize:16];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSString *contact=[contactKeys objectAtIndex:[indexPath section]];
    NSArray *contactSection=[contactNames objectForKey:contact];
    NSMutableArray *sugar=[db sugarId];

    if (isSearchOn) { 
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row]; 
        NSArray *splitText = [cellValue componentsSeparatedByString:@":"];
        NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];
        //NSString *result=[contactText stringByAppendingString:[sugar objectAtIndex:[indexPath row]]];
        firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];                                                  
        firstLabel.tag =40; //This should be a constant probably
        firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
        firstLabel.text = contactText;
        [firstLabel sizeToFit];
        [cell.contentView addSubview:firstLabel];

        sugarLabel = [[UILabel alloc] initWithFrame:
                      CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
        sugarLabel.tag =40; //This should be a constant probably
        //sugarLabel.font = [UIFont boldSystemFontOfSize:16];
        sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
        [sugarLabel setHidden:YES];        
        [cell.contentView addSubview:sugarLabel];


        cell.textLabel.text =firstLabel.text;
        } else {
         NSString *cellText = [contactSection objectAtIndex:[indexPath row]];

            // split the text by the : to get an array containing { "AAA", "BBB" }
            NSArray *splitText = [cellText componentsSeparatedByString:@":"];

            // form a new string of the form "BBB AAA" by using the individual entries in the array
            NSString *contactText = [NSString stringWithFormat:@"%@ %@", [splitText objectAtIndex:1], [splitText objectAtIndex:0]];

            firstLabel=[[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 40)]autorelease];                                                  
            firstLabel.tag =40; //This should be a constant probably
            //firstLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
            firstLabel.text = contactText;
            [firstLabel sizeToFit];
            [cell.contentView addSubview:firstLabel];

            sugarLabel = [[UILabel alloc] initWithFrame:
                                    CGRectMake(10+firstLabel.frame.size.width+2, 10, 300, 60)];
            sugarLabel.tag =40; //This should be a constant probably
            //sugarLabel.font = [UIFont boldSystemFontOfSize:16];
            sugarLabel.text = [sugar objectAtIndex:[indexPath row]];
            [sugarLabel setHidden:YES];        
            [cell.contentView addSubview:sugarLabel];

            NSString *result=[NSString stringWithFormat:@"%@ %@",firstLabel.text,sugarLabel.text];
            cell.textLabel.text=result;

    }

alt text

Figure 2: This is modification to the code as suggested by BP. The result is here under..
alt text

EDIT:

Whenever I try to write stringWithFormat or the above statement, the memory is not freed and the labels are getting overlap over one other. Is there a way to solve this problem..please help me..I spent more than half a day trying to figure this but no luck

  • 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-16T14:46:37+00:00Added an answer on May 16, 2026 at 2:46 pm

    If you want to have custom cells, I recommend you subclass UITableViewCell. In the example code you have given, you are creating a new label every single time the tableview asks for the cell for a row at an index path. While you are autoreleasing them, you are also adding them the the contentview of the cell, which increases their retain count and ensures that their lifetime is as long as the tableview’s.

    As cells are reused, you will continue to add more and more labels to it’s content view, until you eventually run out of memory.

    As far as the strange text output, it looks like you are adding a UILabel to one of your string with format calls.

    If you were to do something like the code below, you would see something similar.


    UILabel *label = [[UILabel alloc] init];
    NSLog(@"%@", label);
    [label release];

    Edit: Seeing your edited screenshot, I’m assuming you scrolled down to the ‘S’ section. In which case, you are seeing the labels on top of other labels. Like I mentioned, you are creating a new label every time and putting it on top of the label you created the first (and second, third, etc) time you used the cell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find the proper way to add a custom widget to the
I'm trying add a space before a particular string ( Token for example) by
Trying to add a button to the right side of the toolbar of the
Im trying to add some bindings to ARP table in Linux, in C. Im
I'm trying to write test harness for part of my Android mapping application. I
I'm trying to build a C++ extension for python using swig. I've followed the
I'm trying to build a Chrome browser extension, that should enhance the way the
I am trying to redirect to a specific path based on HTTP_HOST or SERVER_NAME
I am trying to load a html page through UIWebview.I need to disable all
I am trying to understand the practical difference during the execution of a program

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.