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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:49:07+00:00 2026-05-30T15:49:07+00:00

I’m trying to find a way to not have code being duplicated when I

  • 0

I’m trying to find a way to not have code being duplicated when I set up custom cells in my table view. I currently have a BaseCell class, subclassed from UITableViewController and then two subclasses of BaseCell: CellTypeOne and CellTypeTwo. This is the sort of setup I’m using at the moment:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *type = [self.rowTypes objectAtIndex:indexPath.row];

    if ([type isEqualToString:@"Type 1"]) {
        CellTypeOne *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];

        cell.picture.image = // An image
        cell.caption.text = // A caption

        cell.author.text = // An author

        return cell;
    }

    else if {[type isEqualToString:@"Type 2"]) {
        CellTypeTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];

        cell.picture.image = // An image
        cell.caption.text = // A caption

        cell.number.text = // A number

        return cell;
    }
}

As some of the properties being set are the same for either cell type, I’m wondering if there’s a way to do something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *type = [self.rowTypes objectAtIndex:indexPath.row];

    id cell;

    if ([type isEqualToString:@"Type 1"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];

        // Cast cell to (CellTypeOne *)?

        cell.author.text = // An author
    }

    else if {[type isEqualToString:@"Type 2"]) {
        CellTypeTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];

        // Cast cell to (CellTypeTwo *)?

        cell.number.text = // A number
    }

    cell.picture.image = // An image
    cell.caption.text = // A caption

    return cell;
}

This way I can do class specific setup for each subclass and then do generic setup for both. The only way I’ve gotten this to work is by casting cell each time I need to use it, something like this:

[(BaseCell *)cell picture].image = // An image

Having to cast cell on each line seems like more work than having a couple of lines of duplicated code to begin with, but I’m just trying to figure out the best way to go about this.

  • 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-30T15:49:09+00:00Added an answer on May 30, 2026 at 3:49 pm

    You can make the cell variable of type BaseCell* to avoid casts outside ifs:

    BaseCell *cell;
    
    if ([type isEqualToString:@"Type 1"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];
        [[cell author] setText:/*an author*/];
    } else if {[type isEqualToString:@"Type 2"]) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];
        [[cell number] setText: /* a number */];
    }
    
    cell.picture.image = // An image
    cell.caption.text = // A caption
    

    Alternatively, you can keep the type of id, and use method invocation syntax rather than member (dot) syntax outside the ifs:

    id cell;
    // ... the code from your post
    [[cell picture] setImage: /* an image */];
    [[cell caption] setText: /* a caption */];
    

    Finally, you can avoid casts and square brackets altogether by declaring additional variables inside your ifs:

    BaseCell *cell;
    
    if ([type isEqualToString:@"Type 1"]) {
        CallTypeOne *cellOne = [tableView dequeueReusableCellWithIdentifier:@"Cell 1"];
        cellOne.author.text = // An author
        cell = cellOne;
    } else if {[type isEqualToString:@"Type 2"]) {
        CellTypeTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"Cell 2"];
        cellTwo.number.text = // A number
        cell = cellTwo;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to loop through a bunch of documents I have to put
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

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.