UITextFields in UITableView, entered values reappearing in other cells - iphone

I have a UITableView with about 20 cells, in each cell there are three UITextFields. I did NOT subclass UITableViewCell, but when setting up each cell I set the textfields tag value to a constant plus the row number. So for each row the tag of the textfield is incremented by one.
When I run the app and enter a value in for example row one, it may reappear on row 12. The behavior is not the same each time i run the app.
I should add that I have an array storing the contents entered in each textfield. When a textfield is edited, I save the new value to the array and when the cell is again requested by the tableview i set the textfields value to that stored in the array.
Does this have something to do with reusing UITableViewCells?
When reusing a cell, can textfields in different rows get the same tag numbers? Say for example that the first cell (textfield tag=1001) is reused on row 12, then we have two textfields with the same tag number. If I then enter a value on row 1 and later load row 12, the value from cell one will be loaded from the array and put in row 12 also.
If this is happening, how do I fix it?
Each cell does not have a reference to the textfield, so I don't think I can edit a textfield's tag value by just having access to the cell which it is in.
EDIT:
Here is the code for UITableView:cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//Modify cell, adding textfield with row-unique index value
cell = [self modifyCellForHoleInfo:cell atIndexPath:indexPath];
}
cell.accessoryType = UITableViewCellAccessoryNone;
// Load value for textfield stored in dataArray
((UITextField *)[cell viewWithTag:1000+indexPath.row]).text = [dataArray objectAtIndex:indexPath.row];
Thanks!

The issue is that when the cell is reused (i.e. dequeueReusableCellWithIdentifier returns non-nil), the cell is returned with the existing UITextView. To maintain the uniqueness of the tags, it'll be better to remove any previous UITextField:
- (void)removeExistingTextSubviews:(UITableViewCell *)cell
{
NSMutableArray *toRemove = [NSMutableArray array];
// I don't know if you have non-TextField subviews
for (id view in [cell subviews]) {
if ([view isKindOfClass:[UITextField class]] && view.tag >= 1000) {
[toRemove insert:view];
}
}
for (id view in toRemove) {
[toRemove removeFromSuperView];
}
}
...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
[self removeExistingTextSubviews:cell];
}
//Modify cell, adding textfield with row-unique index value
cell = [self modifyCellForHoleInfo:cell atIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
// Load value for textfield stored in dataArray
((UITextField *)[cell viewWithTag:1000+indexPath.row]).text = [dataArray objectAtIndex:indexPath.row];
Please note that I haven't compiled the code, but it should serve as a starting point.

Just get the textfield text into Dictionary setObject by label text and again check by label Text assign the text to corresponding Textfield.. here is my code...
//In Interface
NSMutableDictionary *amounts;
amounts =[[NSMutableDictionary alloc]init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MoneyEntryIdentifier";
static NSString *CellNib = #"MoneyEntry";
MoneyEntryTableViewCell *cell = (MoneyEntryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (MoneyEntryTableViewCell *)[nib objectAtIndex:0];
}
UILabel *lblname = (UILabel *) [cell lblMemName];
lblname.tag =100;
UITextField *txtfield = (UITextField *)[cell textAmount];
txtfield.tag =indexPath.row;
[txtfield addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
lblname.text = tabledata[indexPath.row];
txtfield.placeholder = [NSString stringWithFormat:#"%ld",(long)indexPath.row];
if ([amounts valueForKey:lblname.text] != nil) {
txtfield.text = [amounts valueForKey:lblname.text];
} else {
txtfield.text = #"";
}
cell.selectionStyle =UITableViewCellSelectionStyleNone;
return cell;
}
-(void)textFieldDidChange:(UITextField *)txtField
{
UILabel *label = (UILabel *)[txtField.superview viewWithTag:100];
NSString *labelString = label.text;
NSString *textFieldString = txtField.text;
[amounts setObject:textFieldString forKey:labelString];
}
Change the nib to your tableview cell...

Related

Erratic contents of UITextField as subviews of table cells when scrolling

I'm trying to add a UITextField as a subview to my table cells. The content of the text fields is fine until I start scrolling and the cells start to be reused. The images illustrate the problem.
At first, the blue values on the right in the UITextField are correct, i.e. the value corresponds to the row number. The second and third images, scrolled down and back up, show that the values are being reused in odd ways.
How do I avoid this? Using unique values for reuseIdentifier solves this problem, but obviously it's not very efficient.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITextField *numberTextField;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(200, 10, 95, 30)];
numberTextField.adjustsFontSizeToFitWidth = YES;
numberTextField.textColor = [UIColor blueColor];
numberTextField.placeholder = #"Enter value";
numberTextField.keyboardType = UIKeyboardTypeDecimalPad;
numberTextField.tag = ([indexPath row]+1);
numberTextField.backgroundColor = [cell backgroundColor];
numberTextField.textAlignment = NSTextAlignmentRight;
numberTextField.clearButtonMode = UITextFieldViewModeNever;
numberTextField.clearsOnBeginEditing = YES;
[numberTextField setEnabled:YES];
[cell addSubview:numberTextField];
} else {
numberTextField = (UITextField *)[cell.contentView viewWithTag:([indexPath row]+1)];
}
cell.textLabel.text = [NSString stringWithFormat:#"Row %i",[indexPath row]+1];
numberTextField.text = [NSString stringWithFormat:#"Value: %i",[indexPath row]+1];
return cell;
}
The problem is you only assign the tag to the numberTextField when it is created. If it gets reused, it doesn't get its tag reassigned.
You should use a constant tag number for the UITextField instead of using row+1.

self.tableView reloadData is stacking text in cell label instead of cleaning it first

I am attempting to update a label inside a cell(note, this is NOT the cell's label text. Its another custom label inside of the cell) after the user selects a value from a previous screen and the nav controller popping them back.
However, when I call reloadData, instead of the label in the cell being cleaned and the new value being placed, its actually stacking on top of what was there already. Like if you took the number 200 and placed a 50 on top of it. You get a weird mesh of the 0 and 5 on top of each other.
Any ideas on how to adjust this? Do I have to reset the label's text to "" every view did appear? and if so, what's the best way to do this, I've tried in the cellForRowAtIndexPath method, but no change.
cellforRowAtIndexPath code
// Set up the cell...
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// get the dictionary object
NSDictionary *dictionary = [_groups objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"key"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
//label for currently selected/saved object
_currentSetting = [[UILabel alloc] initWithFrame:CGRectMake(160, 8, 115, 25)];
[_currentSetting setFont:[UIFont systemFontOfSize:14]];
_currentSetting.backgroundColor = [UIColor clearColor];
_currentSetting.textColor = [UIColor blueColor];
_currentSetting.textAlignment = NSTextAlignmentRight;
_currentSetting.text = [NSString stringWithFormat:#""];
_currentSetting.text = [NSString stringWithFormat:#"%# mi",[setting.val stringValue]];
[cell.contentView addSubview:_currentSetting];
return cell
You are recreating the label and re-adding it every time the cell gets refreshed. All of your cell subviews should only be added when you create the cell the first time.
So in your code you create a cell and all subviews the first time. Then if you need a new cell for scrolling or any other reason you get a reusable cell that has already had all the subviews added to it (re-usable...). Then you go through the process of adding the subviews (again) so now that cell contains the subviews from the previous owner (data) of that cell and the new owner (data) of that cell. That is why they appear stacked on top of eachother when you reload the data.
seudo code:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
//Add all subviews here
}
//Modify (only modify!!) all cell subviews here
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *customLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
customLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,44)];
customLabel.tag = 123;
[cell addSubview:customLabel];
} else {
customLabel = (UILabel *)[cell viewWithTag:123];
}
customLabel.text = #"Some nice text";
return cell;
}

cell.textLabel not getting resized

I'm trying to create a Settings for our app. I'm not sure what is happening here. I have a UITableViewSyleGrouped and in each section of the table, there is 1 row. For my particular row, it shows the person's name. If you click on it, then it pushes to a new tableView that has the list of people to choose from, then when you pop back, the label gets updated, but the label is truncated when I go from a smaller name to a bigger name. I'm trying to create a Settings for our app. Some of the fields look like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
if (tableView == _settingsTableView) {
UITableViewCell *cell = nil;
NSNumber *aSection = [_tableArray objectAtIndex:indexPath.section];
if ([aSection integerValue] == SOUNDS)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"SwitchCell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"SwitchCell"] autorelease];
}
cell.textLabel.text = #"Sounds";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:[[Settings sharedInstance] playSounds] animated:NO]; // initialize value from Settings
[switchView addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
else if ([aSection integerValue] == PERSON) {
cell = [tableView dequeueReusableCellWithIdentifier:#"PersonCell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"PersonCell"] autorelease];
}
Person *p = [_personArray objectAtIndex:row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", p.firstName, p.lastName];
NSLog(#"cL: %#", NSStringFromCGRect(cell.textLabel.frame));
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
My PERSON section gives the user the ability to change People. That code in didSelectRowAtIndexPath is
else {
Person *p = [_personArray objectAtIndex:row];
NSUInteger oldRow = [_lastIndexPath row];
if (oldRow != row) {
dmgr.currentPerson = p;
// Put checkmark on newly selected cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Remove checkmark on old cell
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
[_settingsTableView deselectRowAtIndexPath:_lastIndexPath animated:YES];
self.LastIndexPath = indexPath;
// Update the cell
NSIndexPath *path = [NSIndexPath indexPathForRow:0 PERSON];
UITableViewCell *theCell = [_settingsTableView path];
theCell.textLabel.text = [NSString stringWithFormat:#"%# %#", p.firstName, p.lastName];
[theCell setNeedsDisplay];
NSLog(#"ceLL: %#", NSStringFromCGRect(theCell.textLabel.frame));
}
}
What happens is the label is truncated until I click on the label. (e.g. John D... instead of John Doe). Why does the label not get updated?
I tried looking at the frames, and I'm not sure if that has something to do with it or not. My output is:
cL: {{0, 0}, {0, 0}}
ceLL: {{10, 11}, {76, 21}}
The textLabel field of a UITableViewCell is a regular UILabel. You can set this property to cause it to scale down the text to fit:
theCell.textLabel.adjustsFontSizeToFitWidth = YES;
You can also set a minimum font size
theCell.textLabel.minimumFontSize = whatever
Take a look at the Documentation on UILabel it will help you a lot.

data of uitableviewcell overlapping with each other on scrolling

I have a tableview with four sections and all of the sections have two textfields and a label in different rows. I have added some text as placeholder of textfield. Initially the data appears fine but when I scroll the tableview the cell starts to have overlapped data.
My Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row==0) {
UITextField *txtName = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
txtName.placeholder = #"Full Name";
[cell.contentView addSubview:txtName];
[txtName release];
}
else if(indexPath.row==1) {
UITextField *txtEmail = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
txtEmail.placeholder = #"Email";
[cell.contentView addSubview:txtEmail];
[txtEmail release];
}
else if(indexPath.row==2){
cell.textLabel.text = #"Select Date of Birth";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell...
return cell;
}
thanks in advance
Pankaj
You need to create your text fields only in the block of code that inits the cell. Remember that the table view recycles cells so as you scroll off the screen you get a reused and recycled cell that already has a textfield. You are then creating a new textfield and overlaying the new textfield on the existing one, hence you get overlapping.
here is your code properly refactored
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//create the textField here, and we will reuse it and reset its data for each row.
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
[cell.contentView addSubview:txtField];
txtField.tag=110; //should declare a constant that uniquely defines your textField;
[txtField release];
}
// Configure the cell...
//ok, now we retrieve the textField and set its data according to the row.
UITextField *txtField = (UITextField *)[cell.contentView viewWithTag:110];
if(indexPath.row==0) {
txtField.placeholder = #"Full Name";
}
else if(indexPath.row==1) {
txtField.placeholder = #"Email";
}
else if(indexPath.row==2){
txtField.placeholder = nil; //? did you mean to set something here?
cell.textLabel.text = #"Select Date of Birth";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
I have just modified the previous one for missing an else condition which made an bad access error. The modified code is below:
You need to create your text fields before the block of code that inits the cell and init and add this text field in the block of code that inits the cell. Remember that the table view recycles cells so as you scroll off the screen you get a reused and recycled cell that already has a textfield. You are then creating a new textfield and overlaying the new textfield on the existing one, hence you get overlapping.
here is your code properly refactored
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITextField *txtField;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//init the textField here, and we will reuse it and reset its data for each row.
txtField = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
[cell.contentView addSubview:txtField];
txtField.tag=110; //should declare a constant that uniquely defines your textField;
[txtField release];
}
else{
// if the textfield is alread created and now dequed
//ok, now we retrieve the textField and set its data according to the row.
txtField = (UITextField *)[cell.contentView viewWithTag:110];
}
if(indexPath.row==0) {
txtField.placeholder = #"Full Name";
}
else if(indexPath.row==1) {
txtField.placeholder = #"Email";
}
else if(indexPath.row==2){
txtField.placeholder = nil; //? did you mean to set something here?
cell.textLabel.text = #"Select Date of Birth";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}

UITableView cell/data disappear

I have a segmented tableView that loads all the data in all the cells of all the sections.
There is a textField in each cell.
The tableview doesn't fit the iPad screen completely, and I can't access all the invisible cells in order to read/save data. And when I make changes in "textField", then scroll up, the scroll down, all the changes are gone.
I need to load all the cells, even invisible once, to be able to access them.
I am sorry, I just started working with tables a few days ago...
I think that this problem has something to do with reusable cells, but not sure how to resolve it.
Looking for your help, please.
initialization:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400, 30)] ;
textField.enabled = NO;
cell.accessoryView = textField;
[textField release];
}
UITextField *textField = (UITextField*)cell.accessoryView;
if(indexPath.section == 0)
cell.textLabel.text = [idenInfo objectAtIndex:indexPath.row];
else if (indexPath.section == 1)
cell.textLabel.text = [prodInfo objectAtIndex:indexPath.row];
else if (indexPath.section == 2)
cell.textLabel.text = [visInfo objectAtIndex:indexPath.row];
if(indexPath.section == 0)
textField.text = [idenInfoRez objectAtIndex:indexPath.row];
else if (indexPath.section == 1)
textField.text = [prodInfoRez objectAtIndex:indexPath.row];
else if (indexPath.section == 2)
textField.text = [visInfoRez objectAtIndex:indexPath.row];
textField = nil;
return cell;
}
First of all : you don't have to load all the cells including the invisible ones. That's the whole point of the UITableView and MVC Pattern : separate your views from your data.
What you'll want to do is update your Data source (that is idenInfoRez, prodInfoRez and vizInfoRez in your case) when the user has changed a value inside a textField. So you'll have to set your UIViewController as the delegate of each textfield and update the values as the user types in.
[UIView beginAnimations:#"ShiftUp" context:nil];
[UIView setAnimationDuration:0.0001];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = [indexPath section];
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = objCustCell;
cell.selectionStyle = UITableViewCellSelectionStyleNone ;
}
if (section == 0) {
switch (indexPath.row) {
case 0:{
cell.lable.text = #"Date of Birth";
cell.field.placeholder = #"Birth Name";
break;
}
case 1:{
cell.lable.text = #"Enter Your Name";
cell.field.placeholder = #"Full Name";
break;
}
default:
break;
}
}
[UIView beginAnimations:#"ShiftUp" context:nil];
[UIView setAnimationDuration:0.0001];
// [UIView beginAnimations: #"ShiftUp" context:nil];
NSLog(#"Load cellfor raw at index ");
// Configure the cell.
return cell;
}
Note : UIView animation will not allow text field to move away data or any UIcontroller will remain same in its old state !!
Don't Commit animation otherwise it will not working !!
What you can do is this: in the Editing Changed event of each of the TextField store the value contain in the text field in an NSMutableArray whose number equal the number of cells. i.e.
-(IBAction) EditingChanged: (id) sender
{
UITextField *txt =(UITextField*)sender;
NSString* str =[[NSString alloc] initWithString: txt.text];
//get current text string
NSInteger path =[tableView indexPathForSelectedRow].row;
// get currently selected row, this could be a bit different depending on the number of sections
[yourMutableArray insertObject: str atIndex: path];
[str release]
}
You can then populate the TextField with the values from the NSMutableArray anytime the cells are recreated i.e.
(UITableViewCell *) tableView: (UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
...... // create the cells here and use viewTag to get the textFields
textField.text= [yourMutableArray objectAtIndex:indexPath.row];
//This may be a bit different depending on the number of sections.
}
Also, note that it might be advisable to initialize yourMutable array to the capacity of the number of cells.
I am sorry if the codes are not well formatted as this is my first post on stackoverflow - also there might be some typos in the code. Hope this helps someone.
every time we allocate the cell to different data,the data will not reloading the cell,every time the data override previous data, before allocate the cell to clear cell,
like as cell=nil
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
//it clear data in the cell
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400, 30)] ;
textField.enabled = NO;
cell.accessoryView = textField;
[textField release];
}
You're right, the problem is the cells will be reused. There are two solutions to the problem, the quick and dirty one would be to not use reusable cells:
Remove this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
And just leave this:
UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 400, 30)] ;
textField.enabled = NO;
cell.accessoryView = textField;
[textField release];
That should be ok, if you have only a small number of cells in your tableview (about fewer than 50).
The better solution would be to leave cell reuse on, and fill their textfields as they are requested. The approach differs from app to app, but you basically never should access the cells directly, and store the data of the cell somewhere else, e.g. an NSArray of NSStrings. You could then manipulate the NSArray. Your cellForRowAtIndexPath method would look something like this:
textField.text = [arrData objectAtIndex:indexPath.row];