-1

Data from textfield duplicates in my tableview cell whenever i scroll the tableview. Textfield repeated for every 6 rows again & again.

How to remove duplicate data from textfield using custom cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier= @"ProductCell";
    PlaceOrderTableViewCell * ProductCell = [ProductsTblView dequeueReusableCellWithIdentifier:Identifier];
    if (ProductCell==nil)
    {
        ProductCell = [[PlaceOrderTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90

3 Answers3

2

It's not duplicate data, you are using UITableViewCell incorrectly. For performance reasons cells are reused after they go offscreen, a UITableViewCell should only be used to display data from the model and when it gets reused the previous data is replaced by the one from the new data model entity

You should add a bit more information to your question, How are you setting the values of the cells?

EDIT

If you need to get the value from an UITextField you should implement the UITextFieldDelegate specifically textFieldDidEndEditing: Also you should take a look at this answer which explains a simple UITableView with UITextField implementation.

Also this answer as some interesting tutorials you might want to check

Community
  • 1
  • 1
Benjamin Jimenez
  • 984
  • 12
  • 26
1

The reason why that happens, is because the cellsget reused, to improve the speed of your tableView. If you would have like over 1000 cells, then it would have to load all of them then display them. When it gets reused, it loads only the ones that have to be displayed.

A workaround for this is to either create an array and do like this:

[textField setText:[myArray objectAtIndex:indexPath.row]];

But for more flexibility and time consuming would be to create an NSDictionary, and store the appropriate text for the appropriate indexPath. Like this:

[textField setText:[my_dict objectForKey:indexPath.row];
0

When you are getting same data into your textfield , its means that you are getting dequeue cell, u need to change your model class to give textfield data based on index path of the cell, something like this

textfiled.text = [data objectAtIndex:indexPath.row];

Note: Don't assign textfield.text value at the time of cell creation, this may be another reason , you r getting same data.

MobileApp Developer
  • 384
  • 1
  • 3
  • 12