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