Text in UITextField disappear when scrolling in custom cell (xib)

I have read a lot about my issue and I still cannot pointout the exact problem. I have tried some if the code here in stack but it I keep getting errors. The code below works but vanish the text on scrolling.

Any ideas?


@interface GetQuestionsCustomCell : UITableViewCell <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UILabel *labelQuestion;
@property (strong, nonatomic) IBOutlet UITextField *textFieldAnswer;
@property (strong, nonatomic) IBOutlet UILabel *labelQuestionNumber;
- (IBAction)KeyDown:(id)sender;



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"GetQuestionsCustomCell" owner:self options:nil];
        self = [nibArray objectAtIndex:0];
        self.textFieldAnswer.delegate = self;
    return self;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"Cell";

    GetQuestionsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[GetQuestionsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSInteger nextIndexPathRow = indexPath.row;
    cell.labelQuestion.text = [arrayOfQuestions objectAtIndex:indexPath.row];
    cell.labelQuestionNumber.text = [NSString stringWithFormat:@".%d", nextIndexPathRow];

    switch (indexPath.row) {
        case 0:
            tfA1 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 1:
            tfA2 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 2:
            tfA3 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 3:
            tfA4 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 4:
            tfA5 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 5:
            tfA6 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 6:
            tfA7 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 7:
            tfA8 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 8:
            tfA9 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 9:
            tfA10 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 10:
            tfA11 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
        case 11:
            tfA12 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];

    return cell;


The exact problem is you are neither saving the value of the textfield, nor setting them on cellForRowAtIndexPath

When the user has entered something in the textfield, you need to track the value to somewhere, maybe in an array in your ViewController indexed by question number. One way would be to track the cell in cellForRowAtIndexPath:

cell.textFieldAnswer.tag = indexPath.row;
cell.textFieldAnswer.delegate = self;

Then you need to implement - (void)textFieldDidEndEditing:(UITextField *)textField in your viewcontroller. Save the value there as:

[self.answers replaceObjectAtIndex:textField.tag withObject: textField.text];

Then you need to set the value again in your cellForRowAtIndexPath as [cell.textFieldAnswer setText:[self.answers objectAtIndex:indexPath.row]]

The reason is, iOS will not create 1000 cells if you have 1000 questions. It will reuse the cells. You need to learn about tableView a bit more: TableView Programming Guide

Hope this helps.

Need Your Help

Django: get the first object from a filter query or create

django django-models django-queryset

In Django, queryset provides a method called get_or_create that either returns an objects or creates an object.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.