I have a UITableView
with bunch of UITableViewCell
s. These UITableViewCell
s have multiple UITextField
s inside them.
Now every time I scroll up and down, and the UITableViewCell
s go out of the view, and come back in, whatever text I had entered inside the UITextField
disappears. What is the best way to make this work?
static NSString *CellIdentifier = @"Cell";
ComplaintsCustomCell *cell=(ComplaintsCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ComplaintsCustomCell" owner:self options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ComplaintsCustomCell *) currentObject;
break;
}
}
}
cell.durationFld.text=[dict valueForKey:@"Duration"];
[cell.durationFld setTag:indexPath.row + 5000];
Your code seems to have two issues:
[dict valueForKey:@"Duration"]
[dict valueForKey:@"Duration"]
actually has no value, which explains why the text fields are empty as soon as your code is called again. I think you should create a simple class that has a property to replan NSArray and initialize it with whatever the initial values of the text fields are. Whenever a text field changes, just replace the array object at the same index of indexPath.row with the new text. In your original cellForRowAtIndexPath method, you should assign the text field's text like this:
cell.durationFld.text = [valuesArray objectAtIndex:indexPath.row];
If you deal with a lot of text fields, I'd also strongly recommend taking a look at the free Sensible TableView framework. Good luck!
create an NSMutableArray
which at each position, holds an NSString
object which matches each cell.
when you config + show each cell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
, set the text of the UITextField to the string in the array at position indexPath.row.
when editing a UITextField
, insert/update the NSString
object at the current indexPath.row
position in the array
General Suggestion:
Put/Create UITextField
in cellForRowAtIndexPath
method and add with
[cell.contentView addSubview:textFieldName]
This code write between
if(cell == nil)
{
// put UITextField here
}
in cellForRowAtIndexPath
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.