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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:10:03+00:00 2026-06-04T21:10:03+00:00

I’m subclassing UITableViewCell to have one image on the far left, then some text

  • 0

I’m subclassing UITableViewCell to have one image on the far left, then some text in the middle and then some text on the far right.

This UITableViewCell subclass will only have two images depending on a condition. What I’ve done before when I have used images in UITableViewCell subclass I have declared them as

static UIImage *image = nil; 

and then in the +(void)initialize method I have assigned them. This is made only to have a class instances of the images and not using up resources to assign these images to every instance of UITableViewCell.

So my question is now, depending on a condition in my UIViewController that is calling the cellForRowAtIndexPath method I should show a UITableViewCell with imageA or imageB. So in pseudo-code it would look something like this

if (conditionA) { 
    // Set image of cell to be imageA 
} else { 
    // Set image of cell to be imageB 
}

So how do I achieve this with a subclassed UITableViewCell? I was thinking of one way would be to observe a property in the UITableViewCell and when that property would change I would set the cell image accordingly but that seems to be a little bloated for something that should be pretty easy?

The ideal way would be to set it in the init method of the UITableViewCell but then I would not be able to reuse the cells right?

  • 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-06-04T21:10:04+00:00Added an answer on June 4, 2026 at 9:10 pm

    You shouldn’t worry about redundant images being created for each instance. The UIImage class already has an inbuilt cache, and it does not create a new image instances as long as you are creating them using the imageNamed: selector.

    From the docs for the imageNamed: method in UIImage,

    This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

    If the two images are predefined and won’t change, then you can simplify the subclasses cell’s interface. Only allow the users of this subclass to tell it which type of image should be used. You could use an enum for that.

    In the UITableViewCell’s subclass header, create the enum. Use whatever names suits your use-case.

    typedef enum {
      ImageTypeOne,
      ImageTypeTwo
    } ImageType;
    
    @property (nonatomic) ImageType imageType;
    

    Inside the implementation, override the generated setter as:

    - (void)setImageType:(ImageType)newImageType {
        imageType = newImageType;
    
        if (imageType == ImageTypeOne) {
            theImageView.image = [UIImage imageNamed:@"one"];
        } 
        else if (imageType == ImageTypeOne) {
            theImageView.image = [UIImage imageNamed:@"two"];
        }
        // this will resize the image view to fit the image.
        [theImageView sizeToFit];
    }
    

    Inside your cellForRowAtIndexPath: method, simply set this property depending on the condition.

    if (conditionA) { 
        myCustomCell.imageType = ImageTypeOne;
    } else { 
        myCustomCell.imageType = ImageTypeTwo;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in

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.