UITableView与自定义UITableViewCells混合滚动内容

问题描述:

我有一个异构tableView与整个部分组成的UITableViewCells与UITextField控件在每个人的contetView。问题在于,当我滚动时,我看到文本字段中的文本被混合在一起。UITableView与自定义UITableViewCells混合滚动内容

在这个问题出现之前,我已经见过很多次了,虽然已经废弃了,但我通过为每种单元格分配了不同的cellIdentifier来解决这个问题。

现在有什么不同呢是我动态地创建单元格,并且根据服务器的答案,表格部分可能有零个或多个自定义单元格(具有textField的单元格)。现在

,这很有趣地看到,textfiled的属性,如占位符是整个卷轴执着,但我写的文字不具有持续性可言,文字改变它的位置...

我有一个视频here所以你可以看到我的意思。

我使用的代码是这样的:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    NSString *varCellID = [NSString stringWithFormat:@"%i_VarCellID", (indexPath.row + 7891)]; 

    if (indexPath.section == 0) { 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      [self configureCellViews:cell indexPath:indexPath]; 
     } 
     [self configureCellContent:cell indexPath:indexPath]; 
     return cell; 
    } 
    else if (indexPath.section == 1) { 
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      [self configureCellViews:cell indexPath:indexPath]; 
     } 
     [self configureCellContent:cell indexPath:indexPath]; 
     return cell; 
    } 
    else { 
      UITableViewCell *varCell = [tableView dequeueReusableCellWithIdentifier:varCellID]; 

     if (varCell == nil) { 
      varCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:varCellID] autorelease]; 
      varCell.selectionStyle = UITableViewCellSelectionStyleNone; 
      [self configureCellViews:varCell indexPath:indexPath]; 
     } 
     [self configureCellContent:varCell indexPath:indexPath]; 
     return varCell; 
    } 
} 

我知道这不是优雅,但woked之前。除了dinamically创建的单元格标识符是我(绝望)尝试在每个textField上保留单独内容之一。

下面是如何创建单元格的观点和内容:

#pragma mark - 
#pragma mark ConfigureCellcontent 

- (void)configureCellViews:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath { 

    NSArray *aSection = [sections objectAtIndex:indexPath.section]; 
    NSDictionary *aField; 
    int varformatid; 

    if (indexPath.section == 0) { 

     cell.detailTextLabel.textAlignment = UITextAlignmentLeft; 
    } 
    else if (indexPath.section == 1) { 

     cell.detailTextLabel.textAlignment = UITextAlignmentLeft; 
    } 
    else { 

     aField = [aSection objectAtIndex:indexPath.row]; 
     varformatid = [[aField valueForKey:@"varformatid"] intValue]; 
     NSArray *aSection = [sections objectAtIndex:indexPath.section]; 
     NSLog(@"format id %@", [[aSection objectAtIndex:0] valueForKey:@"varname"]); 

     fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 100, 30)]; 
     fieldLabel.tag = 1234; 
     fieldLabel.textAlignment = UITextAlignmentLeft; 
     fieldLabel.font = [UIFont boldSystemFontOfSize:14]; 
     fieldLabel.adjustsFontSizeToFitWidth = YES; 
     [cell.contentView addSubview:fieldLabel]; 

     theTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 12, 170, 30)]; 
     theTextField.textColor = kAnswersTextColor; 
     theTextField.returnKeyType = UIReturnKeyDone; 
     theTextField.tag = indexPath.row + 101; 
     theTextField.adjustsFontSizeToFitWidth = YES; 
     theTextField.delegate = self; 


     if(varformatid == 6 || varformatid == 7 || varformatid == 8) { 
      // use number pad input 
      theTextField.keyboardType = UIKeyboardTypeNumberPad; 
      [cell addSubview: theTextField]; 
      [theTextField release]; 
     } 
     else if(varformatid == 4) { 
      // use date 
     } 
     else if(varformatid == 17 || varformatid == 18) { 
      // use pull down, elements of pull down are in XML 
     } 
     else if(varformatid == 20) { 
      theSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(120, 8, 100, 30)]; 
      theSwitch.selected = YES; 
      [cell.contentView addSubview:theSwitch]; 
     } 
     else { 
      // use input string 
      [cell addSubview: theTextField]; 
      [theTextField release]; 
     }  
    } 

} 

- (void)configureCellContent:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath { 

    NSArray *aSection = [sections objectAtIndex:indexPath.section]; 
    NSString *aField; 


    if (indexPath.section == 0) { 
     aField = [[aSection objectAtIndex:0] valueForKey:@"SetDate"]; 
     cell.textLabel.text = @"Set Date"; 
     cell.detailTextLabel.text = aField; 
    } 
    else if (indexPath.section == 1) { 
     aField = [[aSection objectAtIndex:0] valueForKey:@"SetType"]; 
     cell.textLabel.text = @"Set Type"; 
     cell.detailTextLabel.text = aField; 
    } 

    else { 

     aField = [[aSection objectAtIndex:indexPath.row] valueForKey:@"varname"]; 
     if ([[[aSection objectAtIndex:indexPath.row] valueForKey:@"varrequired"] isEqualToString:@"yes"]) { 
      [(UITextField *)[cell viewWithTag:indexPath.row + 101] setPlaceholder: @"Required"]; 
     } 
     else [(UITextField *)[cell viewWithTag:indexPath.row + 101] setPlaceholder: @"Optional"]; 
     [(UILabel *)[cell.contentView viewWithTag:1234] setText: aField]; 

    } 
} 

最后,我请求你的帮助,以找到至少一个更好的办法。提前致谢。

你的configureCellContent似乎坏了。所以你要么没有用键入的文本更新你的内部数据结构(你可能希望在每次文本更改时都这样做),否则你会错过在configureCellViews中填写正确的值。

旁注:您不应该在顶部出列两个单元格。只要出队正确的一个!即把它放到如果部分。

+0

谢谢,这帮了我,现在我对输入的文本有了更多的控制。每次为每个textField调用textFieldDidEndEditing:方法时,现在我都有相同数量的节和行的数组来存储文本。尽管如此,我仍然可以看到内容混杂,只有在第一行和最后一行之间才会发生。 – chuckSaldana 2010-08-19 19:49:02

我以前遇到过这个。我相信这是因为UITableViewCells在不可见时被释放以减少内存消耗。当您向后滚动时,您会为该单元格获取新出队的实例。

您需要将单元格的状态保存到某处,以便在返回到视图中时重新初始化该单元格。当你保存完全取决于你 - 当你的UITextField失去焦点时,或者当字段以任何方式改变时。

+0

好点,实际上我通过创建一个镜像数组来解决问题,该数组将包含文本字段中的所有字符串,并且每次创建新单元格时,该方法都会检查相应数组索引中是否有内容将其放入文本字​​段中。 。 – chuckSaldana 2010-10-19 15:20:03

我不知道这是否是您要参加的解决方案,但您可以修改CellIdentifier并使其唯一。

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row]; 

的NSString * CellIdentifier = [NSString的stringWithFormat:@ “格%d”,indexPath.row]; 代替静态NSString * CellIdentifier = @“Cell”;

是在快速滚动的情况下混合行的解决方案。