Reputation: 253
i'm using table view to display loaded array of textfields from server ,so i have a table view list of fields , when i fill this fields of data and scroll down to fill other fields when i scroll up again i found the values changes and there is a duplicated valus
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FieldCell";
//FieldCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
FieldCell *cell = [[FieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//Filling The DataHere
cell.cellImage.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];
cell.cellTextField.placeholder = [placeHolders objectAtIndex:indexPath.row] ;
// Configure the cell...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
return cell;
}
i think the problem in dequeueReusableCellWithIdentifier:CellIdentifier because it re allocate and allocate the cell , but i don't want to do this i want to let every thing in memory with its values
Upvotes: 0
Views: 537
Reputation: 114865
Your cellForRowAtIndexPath
is responsible for obtaining a cell and putting the right value into it - this is why I asked you to show the entire method. The code you have put in your question is not a complete cellForRowAtIndexPath
method.
Update - I have included a simple delegate call back from the cell to the tableview to allow the UITextField delegate in the cell to update the backing store. I didn't use a proper protocol for the delegate because I wanted to throw this together quickly.
FieldCell.h
@class TableView
@property (strong,nonatomic) TableView *delegate;
@property (strong,nonatomic) NSIndexPath indexPath;
@property (strong,nonatomic) UITextField *textField;
FieldCell.c
#import <TableView.h>
#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (self.delegate)
{
[self.delegate updateCellText:[textField.text stringByReplacingCharactersInRange:range withString:string] forIndexPath:self.indexPath;
}
return YES;
}
TableView.h
@property (strong,nonatomic) NSMutableArray *cellTexts; // You need to initialise this as an array full of empty NSStrings or whatever the initial cell values should be
- (void) updateCellText:(NSString *)text forIndexPath:(NSIndexPath)indexPath;
TableView.c
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FieldCell";
FieldCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.indexPath=indexPath;
cell.delegate=self;
cell.textField.text=[self.cellTexts objectAtIndex:indexPath.row]; // modify this as required to actually put your data to be displayed into the cell's properties
return cell;
}
- (void) updateCellText:(NSString *)text forIndexPath:(NSIndexPath)indexPath
{
[self.cellTexts replaceObjectAtIndex:indexPath.row withObject:text];
}
Upvotes: 3
Reputation: 45490
To answer your question
dequeueReusableCellWithIdentifier:CellIdentifier how to avoid it ?
FieldCell *cell = [[FieldCell alloc]init];
This will allocate memory to each cell and will not reuse them but...
By all means , please please don't do this, it will give you a poor performance memory wise, you will even notice a lag when scrolling the table.
The real solution to your problem is not to rely on the cell content since they are being reused, instead you should manage to store the UITextfield
value in your model as you input them.
Upvotes: 3