Objective c UITableView与自定义UITableViewCells在滚动时混合内容

Objective c UITableView与自定义UITableViewCells在滚动时混合内容,objective-c,iphone,uitableview,Objective C,Iphone,Uitableview,我有一个异构的tableView,它的整个部分由UITableViewCells组成,每个部分的contetView上都有一个UITextField控件。问题是,当我滚动时,我看到文本字段中写入的文本混合在一起 我以前见过很多次这个问题,虽然不推荐使用,但我通过为每种单元格分配不同的cellIdentifier来解决这个问题 现在不同的是,我正在动态创建单元格,根据服务器的回答,表的部分可能有零个或多个自定义单元格,其中一个有textField 现在,有趣的是看到TextField的属性(如占位

我有一个异构的tableView,它的整个部分由UITableViewCells组成,每个部分的contetView上都有一个UITextField控件。问题是,当我滚动时,我看到文本字段中写入的文本混合在一起

我以前见过很多次这个问题,虽然不推荐使用,但我通过为每种单元格分配不同的cellIdentifier来解决这个问题

现在不同的是,我正在动态创建单元格,根据服务器的回答,表的部分可能有零个或多个自定义单元格,其中一个有textField

现在,有趣的是看到TextField的属性(如占位符)在整个滚动中都是持久的,但我写的文本根本不是持久的,文本改变了它的位置

我有一个视频,让你明白我的意思

我使用的代码如下所示:

    // 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;
    }
}
我知道它不优雅,但以前做过。除了我不顾一切地试图在每个文本字段中保留单个内容的一个由计算机自动创建的单元格标识符之外

以下是我如何创建单元格视图和内容:

#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似乎已损坏。因此,您可能没有在每次文本更改时使用您可能希望的键入文本更新内部数据结构,或者您没有在ConfigureCellView中填写正确的值


旁注:您不应该让顶部的两个单元格出列。把合适的人排出来!例如,将其放入if部分。

您的configureCellContent似乎已损坏。因此,您可能没有在每次文本更改时使用您可能希望的键入文本更新内部数据结构,或者您没有在ConfigureCellView中填写正确的值


旁注:您不应该让顶部的两个单元格出列。把合适的人排出来!也就是说,将其放入if部分。

我以前遇到过这种情况。我相信这是因为UITableViewCells在不可见时被释放,以减少内存消耗。当您向上滚动时,您将得到该单元的一个新的出列实例


您需要将单元格的状态保存到某个位置,以便在它返回视图时重新初始化它。保存的时间完全取决于您自己—当UITextField失去焦点时,或者当该字段以任何方式更改时。

我以前遇到过这种情况。我相信这是因为UITableViewCells在不可见时被释放,以减少内存消耗。当您向上滚动时,您将得到该单元的一个新的出列实例


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

我不知道这是否是您正在使用的解决方案,但您可以修改CellIdentifier并使其唯一

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

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

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

NSString*CellIdentifier=[NSString stringWithFormat:@Cell%d,indexath.row]; 代替静态NSString*CellIdentifier=@Cell


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

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


是快速滚动时混合行的解决方案。

谢谢,这对我很有帮助,现在我对输入的文本有了更多的控制。现在,每当为每个textField调用TextFieldDiEndEditing:方法时,我都有一个相同数量的节和行的数组来存储文本。尽管如此,我仍然可以看到内容是混合的,而且只发生在第一行和最后一行之间。谢谢,这帮助了我,现在我对输入的文本有了更多的控制。现在,每当为每个textField调用TextFieldDiEndEditing:方法时,我都有一个相同数量的节和行的数组来存储文本。尽管如此,我仍然可以看到内容是混合的,并且只在第一行和最后一行之间发生。好的一点,实际上我通过创建一个镜像数组来解决问题,该数组将包含文本字段中的所有字符串,每次我创建新单元格时,该方法都会检查相应数组的索引中是否有内容,以将其放入textfield…好的方面,实际上我通过创建一个镜像数组来解决问题,该数组将包含textfields中的所有字符串,每次我创建新单元格时,该方法都会检查相应数组的索引中是否有内容,以将其放入文本字段中。。。