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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:04:54+00:00 2026-05-24T11:04:54+00:00

I’m attempting to have a UITableView that I can dynamically add and remove rows

  • 0

I’m attempting to have a UITableView that I can dynamically add and remove rows from, and the rows have a UITextField in them. For adding the rows, I’m using the code:

- (void) addRow
    {
        [nameArray addObject:[NSString stringWithFormat:@"%i", x]];
        [self.tableView reloadData];
        x++;
    }

And I’m just doing a count of nameArray to get how many rows I have in my tableView. Then, inside of cellForRowAtIndexPath, I’ve got the following code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    /*Default Apple-y stuff*/
    ...
    if ([indexPath row] == 0) {
        playerTextFieldZero = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 185, 30)];
        playerTextFieldZero.adjustsFontSizeToFitWidth = YES;
        playerTextFieldZero.placeholder = @"Name";
        playerTextFieldZero.textColor = [UIColor blackColor];
        playerTextFieldZero.returnKeyType = UIReturnKeyDone;
        playerTextFieldZero.backgroundColor = [UIColor whiteColor];
        playerTextFieldZero.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        playerTextFieldZero.textAlignment = UITextAlignmentLeft;
        playerTextFieldZero.tag = 0;
        playerTextFieldZero.delegate = self;
        playerTextFieldZero.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
        [playerTextFieldZero setEnabled: YES];
        [cell addSubview:playerTextFieldZero];
        [playerTextFieldZero becomeFirstResponder];
        [playerTextFieldZero release];
    }
    ...
    /*More of those in here*/
    ...
return cell;
}

I’ve got multiple issues with this code. The first issue is I’m doing a preset number of UITextFields, so that I can call them all in textFieldShouldReturn. Is there a good way for me to generate UITextFields that will return when I press the done key?

The second biggest issue with the way I’m doing this right now is my UITextFields get cleared every time I add a new one. Any idea why?

  • 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-24T11:04:55+00:00Added an answer on May 24, 2026 at 11:04 am

    To solve your first issue I would begin by pulling the UITextField creation code into a method..

    - (UITextField*)textFieldForCell:(UITableViewCell*)cell withDelegate:(id<UITextFieldDelegate>*)delegate {
        UITextField textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 185, 30)];
        textField.delegate = self;
        ....
        [cell addSubview:playerTextFieldZero];
        [textField release];
    }
    

    Then invoke the new method in your tableView:cellForRowAtIndexPath: method…

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
        // Custom initialization code    
        [self textFieldForCell:cell withDelegate:self];
    }
    

    Now to make sure that your UITextField’s respond to the return key implement the textFieldShouldReturn: method of your UITextFieldDelegate (probably your UITableViewController) to always return true…

    -(bool)textFieldShouldReturn:(UITextField*)textField {
        return YES;
    }
    

    As for your second issue, I believe this is a result of directly invoking reloadData. This will force your UITableView to recreate its cells. This in turn recreates your UITextFields and you subsequently lose their state/text. I think your next logical step will be to introduce a model (NSMutableArray) that stores the state of each UITextField. You could begin by saving the text of the field into the array upon the UITextFieldDelegate receiving the textFieldShouldReturn message.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build

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.