1

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?

GetQuestionsCustomCell.h:

@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;

@end

GetQuestionsCustomCell.m:

- (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;
}

SecurityQuestions.m:

- (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;
    nextIndexPathRow++;
    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];
            break;
        case 1:
            tfA2 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 2:
            tfA3 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 3:
            tfA4 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 4:
            tfA5 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 5:
            tfA6 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 6:
            tfA7 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 7:
            tfA8 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 8:
            tfA9 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 9:
            tfA10 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 10:
            tfA11 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
        case 11:
            tfA12 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
            break;
    }

    return cell;
}
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
Idan Moshe
  • 1,675
  • 4
  • 28
  • 65
  • Issue is with cell reusable.. you just need to know how to reuse the cell when there is UITextField inside custom cell.. this may help you http://stackoverflow.com/a/4568460/1059705 and http://stackoverflow.com/q/4045658/1059705 – Bala May 02 '13 at 06:50
  • Can i ask you one thing, you want to get user input from each textFields lays in separate cell? – Augustine P A May 02 '13 at 06:54
  • get each data from text field and save it for later use. – Idan Moshe May 02 '13 at 07:05

3 Answers3

7

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.

Max
  • 3,371
  • 2
  • 29
  • 30
  • Exact way to deal with .. :) – Bala May 02 '13 at 06:54
  • OK, thank you for your answer. I thought about getting the text from the text field when editing done but using - (BOOL)textFieldShouldReturn:(UITextField *)textField and - (void)textFieldDidEndEditing:(UITextField *)textField is a bit problematic for me since I need to set a delegate to text field, but the text field is in custom class, any idea how i deal with it? – Idan Moshe May 02 '13 at 07:01
  • thanks a lot man! oh, cell.textFieldAnswer = indexPath.row; gives me error 'implicit conversion of....' – Idan Moshe May 02 '13 at 07:24
  • 1
    you might mean: cell.textFieldAnswer.tag = indexPath.row; – Idan Moshe May 02 '13 at 07:31
  • not working for the first time.its getting crashed.what about array for the first time. – karthikeyan Jan 27 '16 at 08:06
  • replaceObjectAtIndex may cause a crash unless you initialize the array and prefill. Since you cant store nil in an array you have to store NSNull. So if you have `n` rows, you should run `[self.answers addObject:[NSNull null]]` inside a loop for `n` times. Or use an NSMutableDictionary. In this example dictionary would fit perfectly with question being the key and answer being the value. – Max Jan 27 '16 at 11:56
1

Chaneg CellForRowIndexPath...

- (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;

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


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

[tfA1 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA2 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA3 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA4 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA5 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA6 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA7 setText:[self.answers objectAtIndex:indexPath.row]];
  }
   return cell;
}
hitesh
  • 374
  • 1
  • 7
0

Here is a simple workaround using a tableView delegate method, with this save of your UITextview.text value in this method,

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

//Save the textField values

}
Frostmourne
  • 156
  • 1
  • 19