0

I have customized uitableviewcell, which has two uitextfields. I have inputted some data into cells. However, the data is removed when I scroll down the tableview. I know this happens due to the cell is recreated. How can I keep those inputted data? Here is the code

valueTableViewCell*cell=[[valueTableViewCell alloc]init];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"valueTableViewCell" owner:cell options:nil];
cell = [nib objectAtIndex:0];
return cell;
  • 1
    Possible duplicate of [UITableView cell/data disappear](http://stackoverflow.com/questions/5648191/uitableview-cell-data-disappear) – Tamás Sengel Apr 09 '17 at 21:41

1 Answers1

1

You can use models: e.g some objects stored in array. You can store your data in the models and each time when your cell will be reused, you should update your data in the cell.

Quick pseudo code:

//someObject: 
@property (strong, nonatomic) NSString *firstString;
@property (strong, nonatomic) NSString *secondString;

and in your class with table:

@property (strong, nonatomic) NSArray *dataSource; //dataSource -> someObjects

//...
//...
//...
cell.firstTextField.text = _dataSource[indexPath.row].firstString;

cell.secondTextField.text = _dataSource[indexPath.row].secondString;

return cell;

UPD:

@DaoXingLi I've made quick example for you. Please see the link

Alex V
  • 13
  • 2
  • Thanks, but the data in a cell is not preset. I create empty cells. Then I input data into textfield. After this, when I scroll the uitableview, the data is gone. – DaoXing Li Apr 15 '17 at 01:51
  • @DaoXingLi I've made quick example for you. Please see the [link](https://www.dropbox.com/s/ageydmpgksbcgx7/CellsReuseTest.zip?dl=0) – Alex V Apr 15 '17 at 22:29