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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:26:10+00:00 2026-05-23T04:26:10+00:00

I’m having an issue programmatically adding a subview to a UITableView cell. I am

  • 0

I’m having an issue programmatically adding a subview to a UITableView cell. I am attempting to add a UITextFiled to 2 cells and a UILabel to another cell in one section of my table. The table has 3 sections and most of it is manually created. One of the cells, when selected calls an action that inserts four more cells. If the same cells is selected again, the four cells will be removed. When this action occurs, the subviews get mixed up and are on the wrong cells. The keyboard also does not respond to the done key.

I try to create the subviews inside the if (cell == nil) conditional statement. Here is my code that creates the cells (sorry for it being so messy, I’ve been trying a ton of things):

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSLog(@"cell created");
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (indexPath.section == 0 && indexPath.row == 0 && ([cellText objectAtIndex:indexPath.row] == @"Weight")) {
        exerciseWeight = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, 295, 30)] autorelease];
        exerciseWeight.textAlignment = UITextAlignmentRight;
        exerciseWeight.adjustsFontSizeToFitWidth = YES;
        exerciseWeight.textColor = [UIColor blackColor];
        exerciseWeight.backgroundColor = [UIColor clearColor];
        exerciseWeight.tag = 89899;
        exerciseWeight.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseWeight setReturnKeyType:UIReturnKeyDone];
        exerciseWeight.clearButtonMode = UITextFieldViewModeNever;
        [exerciseWeight setEnabled: YES];
        [exerciseWeight setDelegate:self];
        [cell addSubview:exerciseWeight];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;

    } else if (indexPath.section == 0 && indexPath.row == 1) {
        NSLog(@"Created metric subview");
        metric = [[[UILabel alloc] initWithFrame:CGRectMake(100, 5, 180, 30)] autorelease];
        metric.textAlignment = UITextAlignmentRight;
        metric.textColor = [UIColor grayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        metric.tag = 89898;
        [cell addSubview:metric];
    } else if (([cellText count] - 1) == indexPath.row){
        exerciseReps = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, 295, 30)] autorelease];
        exerciseReps.textAlignment = UITextAlignmentRight;
        exerciseReps.adjustsFontSizeToFitWidth = YES;
        exerciseReps.backgroundColor = [UIColor clearColor];
        exerciseReps.tag = 89890;
        exerciseReps.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseReps setReturnKeyType:UIReturnKeyDone];
        exerciseReps.clearButtonMode = UITextFieldViewModeNever;
        [exerciseReps setEnabled:YES];
        [exerciseReps setDelegate:self];
        [cell addSubview:exerciseReps];
    } else if (indexPath.section == 2) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
} else {
    exerciseWeight = (UITextField *)[cell viewWithTag:89899];
    metric = (UILabel *)[cell viewWithTag:89898];
    exerciseReps = (UITextField *)[cell viewWithTag:89890];
}
if (indexPath.section == 0) {
    cell.textLabel.text = [cellText objectAtIndex:indexPath.row];
}
// Configure the cell...
if (indexPath.section == 0 && indexPath.row == 0) {
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    exerciseWeight.placeholder = @"160";


} else if (indexPath.section == 0 && indexPath.row == 1) {
    metric.text = metricUnit;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (cell.textLabel.text == @"Repetitions"){
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    exerciseReps.placeholder = @"10";
} else if (indexPath.section == 1) {
    cell.textLabel.text = @"Log New Set";
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.accessoryType = UITableViewCellAccessoryNone;
} else if (indexPath.section == 2) {
    cell.textLabel.text = @"History";

} else if (cell.textLabel.text == @"  Pounds") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Kilograms") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Miles") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @"  Kilometers") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
}

return cell;

// [exerciseWeight release];
// [metric release];
// [exerciseReps release]; 
}
  • 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-23T04:26:11+00:00Added an answer on May 23, 2026 at 4:26 am

    looks like you’re going to have some fun here. But it will be satisfying once you’re done 🙂

    I’m not sure the code you posted is the bit you need to be worrying about. Defining what you see in your table (primarily) are methods like heightForRowAtIndexPath, numberOfSectionsInTableView and numberOfRowsInSection. If you get those right you will get the display you want. So when you tap a cell you will be modifying your underlying data structures, then calling reloadData – causing the table view to call those methods all over again.

    Regarding laying out those cells I don’t see you using the cell’s contentView at all. Have you taken a look at the Cocoa With Love blog, specifically Easy custom UITableView drawing and UITableView construction, drawing and management (revisited)? I have also used the xib method of defining table view cells as described in the Apple docs, Table View Programming Guide for iOS and given the way Xcode just keeps improving interface builder’s interaction with code would probably continue to do so. I was very much against IB but now see the value in using it since Xcode 4 took away the pain of switching apps to use it, there are a lot fewer opportunities to make mistakes.

    What I’m thinking is that when you “add a view” to a cell you should just switch to another cell that you have defined and load the cell, after reloadData, as required. When you want to add cells you change your underlying data structures and let the table display itself.

    On the question of cell identifiers – you can either use one cell identifier and save only the cost of creating a cell, factoring out everything common that you do to all your cells, OR you can create cells with a unique identifier per type of cell and reuse cell variant TK421 when your section/row indicates a TK421 is required. I tend to do the second. I don’t actually know which one performs better, but I’m sure that either is better than no reuse at all.

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

Sidebar

Related Questions

No related questions found

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.