我正在尝试创建一个包含大约20个不同字段的注册表单,这些字段分为四个不同的部分。 我已经为它创建了一个自定义单元格,其中包含一个标签和一个UITextField,我的tableview上有一个字典数组,用于指示标签名称应该是什么,以及texftield的标签(因为根据标签,我使用UIPicker输入数据。)
问题在于,当我开始编辑字段时,向下滚动并备份所有内容都搞砸了,并且在字段上更改它不应该...我发现这显然正在发生,因为我正在出去因此它创建了重复的引用,所以我每次都尝试创建一个新的单元格,但这不起作用,因为它只会初始化一个tableview,其中包含我需要的确切数量的单元格,但只是没有内容的空白单元格(标签和文本字段)无论如何。
什么是最简单的方法,所以我可以为每个单元格保留不同的内存引用,考虑到按下按钮时我必须检索它们的值?
我已经阅读了关于this帖子的建议,但仍然不知道如何创建一系列单元格。
任何帮助?
这是我定义的代码:
NSArray *section1 = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"100",@"tag",@"Title",@"title",@"title",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"First Name",@"title",@"first_name",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"Last Name",@"title",@"last_name",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"Job Title",@"title",@"job_title",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"Email",@"title",@"email",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"Confirm Email",@"title",@"confirm_email",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"Password",@"title",@"password",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"Confirm Password",@"title",@"conf_password",@"fieldName",nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"tag",@"Phone",@"title",@"phone",@"fieldName",nil],
nil];
....
self.formItems = [[NSArray alloc] initWithObjects:
section1,
section2,
section3,
section4,
section5,
nil];
然后我在cellForRowAtIndexPath上使用的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"freeTrialFormCell";
TSIFreeTrialFormCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TSIFreeTrialFormCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray* sectionData = [self.formItems objectAtIndex:indexPath.section];
NSDictionary* myFieldInfo = [sectionData objectAtIndex:indexPath.row];
if ([[myFieldInfo objectForKey:@"fieldName"] rangeOfString:@"password"].location != NSNotFound) {
cell.textField.secureTextEntry = YES;
}
cell.fieldName = [myFieldInfo objectForKey:@"fieldName"];
cell.textField.placeholder = [myFieldInfo objectForKey:@"title"];
cell.textField.tag = [[myFieldInfo objectForKey:@"tag"] integerValue];
cell.label.text = [myFieldInfo objectForKey:@"title"];
return cell;
}
答案 0 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TSIFreeTrialFormCell *cell = [[TSIFreeTrialFormCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
NSArray* sectionData = [self.formItems objectAtIndex:indexPath.section];
NSDictionary* myFieldInfo = [sectionData objectAtIndex:indexPath.row];
if ([[myFieldInfo objectForKey:@"fieldName"] rangeOfString:@"password"].location != NSNotFound) {
cell.textField.secureTextEntry = YES;
}
cell.fieldName = [myFieldInfo objectForKey:@"fieldName"];
cell.textField.placeholder = [myFieldInfo objectForKey:@"title"];
cell.textField.tag = [[myFieldInfo objectForKey:@"tag"] integerValue];
cell.label.text = [myFieldInfo objectForKey:@"title"];
return cell;
}
但是,当您为每一行创建单元格时,这会有性能损失
答案 1 :(得分:0)
UITableView会在您向上和向下滚动时重复使用单元格,因此您将丢失仅键入该单元格的信息。我所做的是创建一个数组来保存这些单元格的输入。
基本上:
enteredText = [[NSMutableArray alloc] init]; //iVar, not local variable
for (int i=0; i<numTableRows; i++) {
[enteredText addObject:@""]; //fill with dummy values that you will replace as user types
}
然后在表格视图文本字段中,将自己设置为文本字段的委托并实现textFieldDidEndEditing
。在该方法中,将该行的enteredText
中的值替换为用户的文本。
制作单元格时,将文本字段文本设置为数组中的值:
myCell.textField.text = [enteredText objectAtIndex:indexPath.row];
一开始它会是空白的,然后在用户放入内容后会保留用户的文字。