1
我正在使用UITableView来显示我的应用程序首选项。
我得到的细胞与此:UITableViewCell中的UiTextField在滚动时变空
- (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 = UITextBorderStyleNone;
}
} 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;
}
的问题是,当一排滚动窗口外,文本字段得到空。 我该如何预防?
我认为缓存是正确的解决方案,但即使有可重用的标识符,我仍然有同样的问题。
你说得对。看起来问题与笔尖文件标识符有关。这就是我讨厌IB的原因。我会以编程方式做旧式的。谢谢! – pasine