0

There are similar questions already asked many times and have referred many solutions but none worked for me as they were addressing fixed number of cells and I have dynamic number of cells. Here is the problem:

It is in Objective-C. I've tableview which has dynamic number of rows(user can add more cells or remove cells as per requirement) and each row contains 3 textfields and at the end I want to get data from that textfields and save them. Getting data from all textfields is working perfectly but here comes main problem when I add data into any row(3 textfields) it's getting repeat when I scroll it.

Here is code:

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if ([_txtNumbers resignFirstResponder]) {
        totalIndex = [_txtNumbers.text integerValue];
        [_tblContacts reloadData];
    }
}

- (IBAction)onAddCellClicked:(id)sender {
    totalIndex = [_txtNumbers.text integerValue];
    totalIndex = totalIndex + 1;
    _txtNumbers.text = [NSString stringWithFormat:@"%ld",(long)totalIndex];
    [_tblContacts reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return totalIndex;
}

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

    static NSString *CellIdentifier = @"InviteEmailTableViewCell";

    InviteEmailTableViewCell *cell = [self.tblContacts dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.txtName.delegate = self;
    cell.txtTell.delegate = self;
    cell.txtEmail.delegate = self;

    UIToolbar  *numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    [numberToolbar setTintColor:kShadowColor1];
    [numberToolbar setBarTintColor:[UIColor blackColor]];
    numberToolbar.items = [NSArray arrayWithObjects:
                           [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad:)],
                           [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                           [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
                           nil];
    cell.txtTell.inputAccessoryView = numberToolbar;

    [cell.txtName setTag:indexPath.row];
    [cell.txtTell setTag:indexPath.row];
    [cell.txtEmail setTag:indexPath.row];

    return cell;
}

what to do next here?

James Z
  • 12,209
  • 10
  • 24
  • 44
dmaulikr
  • 458
  • 5
  • 20
  • Reuse issue. These should help https://stackoverflow.com/questions/52258956/view-is-getting-replaced-while-scrolling-in-uitableview/52259714#52259714, https://stackoverflow.com/questions/52100899/uibutton-image-for-normal-state-in-collectionview-cell-repeats-itself-every-four/52102002#52102002 – Rakesha Shastri Jan 30 '19 at 05:43
  • override the perpareForReuse method of tableview cell in your custom cell class and reset the textfields falues in the this method. – vivekDas Jan 30 '19 at 05:51
  • @vivekDas thank you for response and this is in objC and I tried overriding it but not worked if you can show me how to do in ObjC it would be big help. – dmaulikr Jan 30 '19 at 05:55
  • what you have tried till now for perpareForReuse show the code. – vivekDas Jan 30 '19 at 05:59

2 Answers2

-1

As textfields are getting reused, it is obvious that entered value is going to be getting repeated.

Solution:

  1. Create one array to keep data of each cell/row(with number of rows = array.count).
  2. Keep delegate of every textfield - on every textfield's textFieldDidEndEditing method, save data in array.
  3. In cellForRowAtIndexPath method, for every textfield of each cell, get data from array for given indexpath and set related value for that textfield.
Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
  • thank you for answer but it is still confusing for me as i've tried many things so may be because of confusion I cannot pay attention to what exactly you are saying. if possible can you tell me steps to follow? I know you have written steps but it is not clear for me sorry. – dmaulikr Jan 30 '19 at 06:03
  • First understand your issue, why you are getting this issue. Do you know what is meaning of this line 'dequeueReusableCellWithIdentifier'?, It just means that - the cell which is going to be hide, is going to be reused again. So, it is obvious that textfield value that you have filled already is going to be repeated. – Mehul Thakkar Jan 30 '19 at 06:07
  • So, you cant rely on tableview to store your data. So, you have to create an array to keep the data. – Mehul Thakkar Jan 30 '19 at 06:09
  • ok I got that. So I should save data into array in textFieldDidEndEditing right? and you mentioned that add delegate into textFieldDidEndEditing so should I remove that from cellForRowAtIndexPath? – dmaulikr Jan 30 '19 at 06:22
  • current delegate is perfectly fine.. just telling you that - 1. whenever any textfild's value get changed - save new value into array, 2. In cellForRowAtIndexPath method, get corresponding value from array and set into relevant textfield. – Mehul Thakkar Jan 30 '19 at 06:25
  • ok let me do that and will this work on dynamic number of rows? as in this screen there is add button which adds one cell to tableview and also vice versa. – dmaulikr Jan 30 '19 at 06:30
-1

As others have mentioned, your tableview cells are being reused and so are the textfields. Since these textfields are not destroyed and recreated you have to manually reset the state of these fields.

Your datasource should be responsible for managing the view state.

As Mehul mentioned in his answer, keep a datasource to store the text typed in the textfields(using textfield tags as indexPath.row can be helpful), and in cellForRowAtIndexPath update the text of these textfields using your datasource.

This approach ensures synchronization between your views and datasource.

Vinay Jain
  • 1,653
  • 20
  • 28
  • yes I understand that but the situation is that I've 3 textfields into cell and number of cells can be increase or decrease. – dmaulikr Jan 30 '19 at 06:44
  • @Mr.Desai : You can increase or decrease(update) array based on you are increasing or decreasing your cell. – Mehul Thakkar Jan 30 '19 at 13:09