UITableView单元格中的UITextField返回null [英] UITextField in UITableView cell is returning null
问题描述
我已经在这个问题上撞了一下墙了很长一段时间了。非常感谢任何输入或方向。
I've been banging my head against the wall on this one for quite some time now. Any input or direction is greatly appreciated.
因此,目标是从表格中的文本字段创建登录表单。收集后的用户信息将被传递到单独的视图控制器中的数组,因此可以存储在收藏夹列表中。
So the goal is the create a log in form from text fields in a table. That user info, once collected will be passed on to an array in a seperate view controller so in can stored in a "favourites" list.
所以我创建了看起来很棒的表单,但是当我调出表单结果时,字段返回(null)。我已经选择了该网站的答案,但不能确切。下面是cellForRowAtIndexPath:方法我觉得可能是我正在创建textFields的问题。同样,任何输入都很受欢迎!
So I've created the form which looks great and all but when I console out the form results the fields are return (null). I've picked the site for answers but can't anything exact. Below is the cellForRowAtIndexPath: method I feel that maybe the issue is where I'm creating the textFields. Again, any input is appreciated!
- (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方法时都会将其添加到单元格中。由于单元格被重用,这会搞砸。
The textField variable (I assume it is an ivar in the class or a static global variable) is your main problem. You create a new text field each time you create a new cell, which is fine, but then you add it to a cell every time the cellForRowAtIndexPath method is called. Since cells are reused this will screw things up.
你的代码需要看起来像这样:
Your code need to look something like this:
- (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;
}
不确定这完全符合您的要求,但它应该指向右侧方向。
Not sure this does exactly what you want but it should point you in the right direction.
这篇关于UITableView单元格中的UITextField返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!