在这个问题上我已经用头撞墙好一段时间了。非常感谢任何意见或方向。
所以我们的目标是从表格中的文本字段创建一个登录表单。该用户信息一旦被收集,将被传递到单独 View Controller 中的数组,因此可以存储在“收藏夹”列表中。
所以我创建了一个看起来很棒的表单,但是当我控制台输出表单结果时,字段返回(null)。我已经选择了该网站作为答案,但无法提供任何确切信息。下面是 cellForRowAtIndexPath: 方法我觉得问题可能出在我创建 textFields 的地方。再次感谢任何意见!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
[textField retain];
}
textField.adjustsFontSizeToFitWidth = NO;
textField.font = [UIFont fontWithName:@"Helvetica" size:14.0];
textField.textColor = [UIColor darkGrayColor];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.textAlignment = UITextAlignmentLeft;
textField.clearButtonMode = UITextFieldViewModeNever;
textField.delegate = self;
if ([indexPath section] == 0) {
switch (indexPath.row) {
case 0:
textField.placeholder = @"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 0;
break;
case 1:
textField.placeholder = @"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 1;
break;
case 2:
textField.placeholder = @"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 2;
break;
case 3:
textField.placeholder = @"";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 3;
break;
case 4:
textField.placeholder = @"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 4;
break;
case 5:
textField.placeholder = @"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.tag = 5;
break;
}
}
[textField setEnabled: YES];
[cell addSubview:textField];
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editing = YES;
return cell;
}
最佳答案
textField 变量(我假设它是类中的 ivar 或静态全局变量)是您的主要问题。每次创建新单元格时都会创建一个新的文本字段,这很好,但是每次调用 cellForRowAtIndexPath 方法时都会将它添加到单元格中。由于细胞被重复使用,这会把事情搞砸。
您的代码需要如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editing = YES;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
textfield.tag = 1;
textField.adjustsFontSizeToFitWidth = NO;
textField.font = [UIFont fontWithName:@"Helvetica" size:14.0];
textField.textColor = [UIColor darkGrayColor];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.textAlignment = UITextAlignmentLeft;
textField.clearButtonMode = UITextFieldViewModeNever;
textField.delegate = self;
[textField setEnabled: YES];
[cell.contentView addSubview:textField];
[textField release];
}
UITextField *textField = (UITextField *) [cell.contentView viewWithTag:1];
if ([indexPath section] == 0) {
switch (indexPath.row) {
case 0:
textField.placeholder = @"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 1:
textField.placeholder = @"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 2:
textField.placeholder = @"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 3:
textField.placeholder = @"";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 4:
textField.placeholder = @"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 5:
textField.placeholder = @"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeNumberPad;
break;
}
}
textField.text = [listData objectAtIndex:indexPath.row];
return cell;
}
不确定这是否完全符合您的要求,但它应该能为您指明正确的方向。
关于objective-c - UITableView 单元格中的 UITextField 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4045658/