UiTextField in a UITableViewCell get empty when scrolling
I'm using a UITableView grouped to display my application preferences.
I get the cell with this:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger section = [indexPath section];
NSInteger row = [indexPath row];
NSInteger identifier = row + ((section + 1) * 10);
NSString *CellIdentifier = [NSString stringWithFormat:@"CustomCellIdentifier-%d",identifier];
LoginTableViewCell *cell = (LoginTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoginTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
cell.field.tag = identifier;
if (section == 0) {
switch (row) {
case 0:{
NSString *user = [[NSUserDefaults standardUserDefaults] valueForKey:@"username"];
cell.field.text = user;
cell.label.text = @"User";
break;
}
case 1:{
NSString *bundleName = [[NSBundle mainBundle] bundleIdentifier];
NSError *error = nil;
NSString *user = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
NSString *pwd = [SFHFKeychainUtils getPasswordForUsername:user andServiceName:bundleName error:&error];
cell.field.text = pwd;
cell.field.secureTextEntry = YES;
cell.label.text = @"Password";
break;
}
case 2:{
NSString *numero = [[NSUserDefaults standardUserDefaults] valueForKey:@"numero"];
cell.field.text = numero;
cell.field.keyboardType = UIKeyboardTypePhonePad;
cell.label.text = @"Number";
break;
}
default:
break;
}
if (cell.field.text > 0) {
cell.field.enabled = NO;
cell.field.borderStyle = UITextBo开发者_运维知识库rderStyleNone;
}
} else {
switch (row) {
case 0:{
cell.field.text = [NSString stringWithFormat:@"+%@", [[NSUserDefaults standardUserDefaults] valueForKey:@"prefix"]];
cell.field.keyboardType = UIKeyboardTypePhonePad;
cell.label.text = @"Prefix";
break;
}
case 1:{
cell.field.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"sender"];
cell.field.keyboardType = UIKeyboardTypeNamePhonePad;
cell.label.text = @"Sender";
break;
}
default:
break;
}
}
}
return cell;
}
The problem is that, when a row scrolls outside the window, the textfield get empty. How can I prevent this?
I thought caching was the right solution, but even with the reusable identifier, I still have the same problem.The approach you're using, that is loading the table cell from a nib file, requires - in order to have correct reusable cells support - to assign in the nib "Identifier" field the same string identifier you use in the code. That is, if your cell identifier used for cache deque is "MyCellId" then your UITableCellView "Identifier" field in the nib file must contain this same "MyCellId" identifier. But with the approach you are following this is not possible, as the cell ID is dynamically generated and then you cannot of course dynamically assign the cell identifier to the nib-loaded cell. In theory you must create a specific nib file for each of the MyCellId-%d identifiers you generate. As I assume you have only one table cell defined, get rid of the dynamic identifier generation and use a static name and then assign this name to the "Identifier" field in the cell nib file .
精彩评论