2012-11-29 6 views
1

我试图创建一个注册表单,其中包含大约20个不同的字段分为四个不同的部分。 我为此创建了一个自定义单元格,它包含一个标签和一个UITextField,并且我的桌面视图上有一个字典数组,指示标签的名称应该是什么,以及texftield的标签(因为根据标签我使用UIPicker输入数据)。自定义单元格textfield没有保存在UITableView中的内存

的问题是,当我开始编辑的字段,向下滚动并备份一切变得一团糟,而改变的字段不应该......我碰到这显然是发生了,因为我所以我试图创建一个新的单元格,但这是行不通的,因为它只会初始化我需要的确切数量的单元格的tableview,而只是没有内容的空单元格(标签也不是textfield)里面。

什么是最简单的方法,所以我可以保持我的细胞的每一个不同的内存引用,考虑的事实,我有当按下一个按钮来检索他们的价值观?

我读过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

我忘了提及...我正在使用StoryBoarding。 –

回答

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; 
} 

但是这将有性能损失为您每一行创建细胞

+0

感谢您的回答,但正如我所提到的,这只会创建一堆空单元格,没有标签,也没有字段。我需要在自定义单元类上做些什么吗? –

0

的UITableView的重用细胞,你向上滚动和Dow n,所以你会失去只能输入该单元格的信息。我所做的是创建一个数组来保存这些单元格的输入。

基本上是:

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]; 

这将是空的,然后再将持有用户的文本,他们已经把后有东西在里面。

+0

苏恩喜欢一个好主意,但是不重复使用细胞不是更好吗,因为它不是很多细胞?我已经使用我的自定义tableview控制器类作为委托,所以无论如何它不会做太多的努力来做到这一点。 –