Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView don't reuse cells, good case to do this?

I have 5 cells in a UITableView. Each has a UITextField as a subview, where the user will input data. If I DO use cell reuse, the textfield gets cleared if the cells are scrolled out of view. I don't want to have to deal with this. Is there a way to NOT reuse cells so that I don't have this issue, if so, how?

Is this a bad idea?

like image 293
Nic Hubbard Avatar asked Jun 29 '11 05:06

Nic Hubbard


1 Answers

I have same feature in one of my apps and I used below code to accomplish that and I never had this kind of problem.

First of all you need to store all your textField value temporary in Array. Make array like this.

arrTemp=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],
         [NSString stringWithFormat:@""],nil];

Then Give all textField tag = indexPath.row;

After that You need to replace textField value in below two methods.

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   [arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}

At Last You need to set that value in cellForRowAtIndexPath datasource Method. So that whenever user scroll tableview it set previous value from temp array. Like this.

cell.txtEntry.text = [arrTemp objectAtIndex:indexPath.row];

It might possible I forgot some of the code to paste here. So if you have any problem please let me know.

like image 157
Deeps Avatar answered Nov 07 '22 12:11

Deeps