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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:29:15+00:00 2026-05-26T08:29:15+00:00

I want to create a UITableViewCell with an image between the content and the

  • 0

I want to create a UITableViewCell with an image between the content and the accessory view, sonly if certain condition is met.

So I have to create a custom content view with two UILabel and UIImageView as described in “Table View Programming Guide for iOS”.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UILabel *mainLabel;
    UILabel *secondLabel;
    UIImageView *icon;

    YOEvento *aux = [[self.eventosListsContainer objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        // 1. Check if it is a favourtite to display the icon

        if (aux.isFavourite) {
            // Evento is favourite
            // 1. Create the main label view
            mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 8, 248, 14)] autorelease];
            mainLabel.tag = MAINLABEL_TAG;
            mainLabel.font = [UIFont systemFontOfSize:13.0];
            mainLabel.textColor = [UIColor darkGrayColor];
            mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:mainLabel];

            // Create the date label
            secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 24, 245, 11)] autorelease];
            secondLabel.tag = SECONDLABEL_TAG;
            secondLabel.font = [UIFont systemFontOfSize:11.0];
            secondLabel.textColor = [UIColor lightGrayColor];
            secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:secondLabel];

            // Create the image
            icon = [[[UIImageView alloc] initWithFrame:CGRectMake(268, 12, 24, 21)] autorelease];
            icon.tag = ICON_TAG;
            icon.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:icon];

        } else {
            // Evento is not favourite
            // 1. Create the main label view
            mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 8, 282, 14)] autorelease];
            mainLabel.tag = MAINLABEL_TAG;
            mainLabel.font = [UIFont systemFontOfSize:13.0];
            mainLabel.textColor = [UIColor darkGrayColor];
            mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:mainLabel];

            // Create the date label
            secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 24, 279, 11)] autorelease];
            secondLabel.tag = SECONDLABEL_TAG;
            secondLabel.font = [UIFont systemFontOfSize:11.0];
            secondLabel.textColor = [UIColor lightGrayColor];
            secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
            [cell.contentView addSubview:secondLabel];            

        }
    } else {
        // 
        if (aux.isFavourite) {
            mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
            secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
            icon = (UIImageView *)[cell.contentView viewWithTag:ICON_TAG];
        } else {
            mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
            secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];            
        }
    }

    // Load cell values

    if (aux.isFavourite) {
        mainLabel.text = aux.nombre;
        secondLabel.text = @"16 de Octubre"; // ?????????????
        icon.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"29-heart"
                                                                                      ofType:@"png"]];
    } else {
        mainLabel.text = aux.nombre;
        secondLabel.text = @"16 de Octubre"; // ????????????? 
    }    
    return cell;
}

The problem is that the icon image is loaded randomly because it keeps showing when reusing cells.
I have to admit that I don’t fully understand the reusing cells theory.

In my original implementation (using the imageView property) it was easy, I just check is the event was not a favorite to set cell.imageView = nil. But have no idea how to do with custom contentView.

  • 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-26T08:29:16+00:00Added an answer on May 26, 2026 at 8:29 am

    Once you create your cell, you are wanting to re-arrange its contents – this is adding unnecessary complexity. Instead you should create two different cell types – one for a favourite and one for a non-favourite. Try something like this:

    static NSString *CellIdentifierFavourite = @"CellFavourite";
    static NSString *CellIdentifierNonFavourite = @"CellNonFavourite";
    
    //  Other setup goes here
    
    YOEvento *aux = [[self.eventosListsContainer objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
    if (aux.isFavourite) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFavourite];
        if (cell == nil) {
            //  Create favourite cell here
        }
        // Populate favourite cell here
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierNonFavourite];
        if (cell == nil) {
            //  Create non-favourite cell here
        }
        // Populate non-favourite cell here
    }
    
    //  Rest of method goes below
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would want to create a custom UITableViewCell which should have a different appearance
i want create image animation , i have 50 images with png format now
I want to dynamically create an image for a UITableViewCell which is basically a
I want to know how can i create a custom UITableViewCell with three parts
I want to create a tableviewcell and add it on the view(not tableview) as
I've created a custom UITableViewCell with a label and a text field. I want
i have a tableview which has image and a text behind, i create the
I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view
I have an UITableView and I want to detect double touches on UITableViewCell. I
how to make a UIView added to UITableViewCell content view fit within its bounds?

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.