Reputation: 13
I'm storing data into NSUserDefaults like so from each of my custom UITableView Cells:
for (int i = 0; i < additionalClaimants; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [self.table_view cellForRowAtIndexPath:indexPath];
UITextField* firstNameField = (UITextField *)[cell viewWithTag:1];
UITextField* employeeIDField = (UITextField *)[cell viewWithTag:2];
[defaults setObject:firstNameField.text forKey:[NSString stringWithFormat:@"claimant%d_name",i+1]];
[defaults setObject:employeeIDField.text forKey:[NSString stringWithFormat:@"claimant%d_employeeID",i+1]];
}
[defaults setInteger:additionalClaimants forKey:@"total_add_claimants"];
[defaults synchronize];
I'm displaying the data in the UITableView like so in the cellForIndexPath method:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AdditionalClaimantsTableViewCell *cell = [self.table_view
dequeueReusableCellWithIdentifier:@"Cell"];
NSString *claimant_name = [defaults objectForKey: [NSString stringWithFormat:@"claimant%ld_name", (long)indexPath.row+1]];
NSString *claimant_employeeID = [defaults objectForKey: [NSString stringWithFormat:@"claimant%ld_employeeID", (long)indexPath.row+1]];
cell.txtField_eid.text = claimant_employeeID;
cell.txtField_name.text = claimant_name;
return cell;
}
Problem is when scrolling, the textfields that appear off view seem to lose the data in them.
Upvotes: 0
Views: 1807
Reputation: 21536
Your code in cellForRowAtIndexPath
is fine (subject to clarifying which version of dequeueReusableCellWithIdentifier
you want to use). Your problem lies in the for
loop where you try to save the values. If you debug this for
loop, you will find that cell
is nil for any rows which are no longer on screen. You need to find a way to save the values for each row before (or as soon as) it is scrolled off screen. To do that, use the tableView delegate method:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Check and save the textField values in that method.
You will still need to save the values of any cells that are visible. Your existing for loop will achieve that, but you could optimise it slightly by using the tableView's visibleCells
property to get an array of cells, and iterate through that (which will avoid building the indexPath for rows which are not visible).
Upvotes: 3
Reputation: 8845
Problem is when scrolling, the textfields that appear off view seem to lose the data in them.
There really aren't such things as offscreen cells in table views. Not the way you're imagining them. Once a cell is scrolled offscreen it becomes the next cell to move onscreen again. It gets queued for reuse, and the -dequeueReusableCellWithIdentifier: call grabs it to use again to display another row in the table.
So never assign data to a table view cell except in the -tableView:cellForRowAtIndexPath: delegate call. Ever. Really. Trust me.
Upvotes: 0