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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:38:31+00:00 2026-05-24T08:38:31+00:00

I am subclassing uitableviewcell so that i can apply a standard background and text

  • 0

I am subclassing uitableviewcell so that i can apply a standard background and text for all my cells, this is my first attempt at this but I have it mostly displaying how i would like. Although i am stuck on one issue. My table has two groups and i would like the first group to have the text centered and i would like to have the second group align to the left. But no such luck at this point.

CustomCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self];
NSLog(@"%i", indexPath);
int rows = [(UITableView *)self.superview numberOfRowsInSection:indexPath.section];
NSLog(@"%i", rows);

if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {

    if (centerText) {
        cellText = [[[UILabel alloc] initWithFrame:CGRectMake(0, 15, self.bounds.size.width - 10, 30)] autorelease];
        cellText.textAlignment = UITextAlignmentCenter;
    }else {
        cellText = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, self.bounds.size.width - 10, 30)] autorelease];
        cellText.textAlignment = UITextAlignmentLeft;
    }

    cellText.font = [UIFont boldSystemFontOfSize:16];
    cellText.backgroundColor = [UIColor clearColor];
    cellText.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    cellText.shadowOffset = CGSizeMake(0,1);
    cellText.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0];



    UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.frame];
    UIImage* img = [UIImage imageNamed:@"odd_slice.png"];
    imgView.image = img;
    self.backgroundView = imgView;
    [imgView release];

    UIImage *accessoryImage = [UIImage imageNamed:@"content_arrow.png"];
    UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
    //  accessoryView.image = accessoryImage;
    self.accessoryView = accessoryView;
    [accessoryView release];


    //Selected State
    UIImage *selectionBackground = [UIImage imageNamed:@"row_selected.png"];
    UIImageView *selectionView = [[UIImageView alloc] initWithFrame:self.frame];
    selectionView.image = selectionBackground;
    self.selectedBackgroundView = selectionView;
    [selectionView release];

    //Adds Text
        [self addSubview:cellText];
    }
    return self;
}

TableView.m

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

CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSArray *keys = [[appDelegate rowersDataStore] allKeys];

static NSString *CellIdentifier = @"Cell";

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    switch (indexPath.section) {
        case 0:
            [cell setCenterText:YES];
            break;
        case 1:
            [cell setCenterText:NO];
            break;

        default:
            break;
    }


    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

switch (indexPath.section) {
    case 0:
        [cell.cellText setText:@"Create New Rower"];            
        break;
    case 1:
        [cell.cellText setText:[keys objectAtIndex:indexPath.row]];
        break;

    default:
        break;
}
    // Set up the cell...
    return cell;
}

Anyone have any suggestions?

  • 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-24T08:38:31+00:00Added an answer on May 24, 2026 at 8:38 am

    You’re misunderstanding how reusable cells work. When you call dequeueReusableCellWithIdentifier you’re getting back a cell that was already allocated. It could have been used for either group in your code.

    This is the code that makes no sense:

    if (cell == nil) {
    
        switch (indexPath.section) {
            case 0:
                [cell setCenterText:YES];
                break;
            case 1:
                [cell setCenterText:NO];
                break;
    
            default:
                break;
        }
    
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    

    You’re fetching a cell. If it doesn’t exist, then you set the centering (which does nothing; remember, cell is nil). You then create one.

    But what you really mean to do is to set the centering after you’ve fetched or created the cell. You don’t care how you get it; you just want to reconfigure it for the current values.

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    switch (indexPath.section) {
        case 0:
            [cell setCenterText:YES];
            [cell.cellText setText:@"Create New Rower"];            
            break;
        case 1:
            [cell setCenterText:NO];
            [cell.cellText setText:[keys objectAtIndex:indexPath.row]];
            break;
    
        default:
            break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create a UITableView, that contains richly formatted text within each UITableViewCell.
I have a grouped UITableView with custom cells (created by subclassing UITableViewCell ). I
I'm subclassing NSUserDefaults in my application. A side effect of this is I can't
I have a Fixed Point type by subclassing Type (refer to this post ).
I have a UITableViewController and a MyTableViewCell subclassing UITableViewCell . I want to inform
I have a UIButton in a customized UITableViewCell, which subclassing UITableViewCell. The UIButton is
how can I change the position of a UITableViewCell content view without subclassing 'UITableViewCell'?
When should - (void)layoutSubviews appropriately be used when subclassing a UITableViewCell. I noticed that
I'm subclassing UITableViewCell and adding two subviews (image, UILabel). When the row is touched.
I'm subclassing the standard ListBox control. I get notified of changes to any of

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.