Ios UITableViewCell在滚动时复制UITextfield子视图

Ios UITableViewCell在滚动时复制UITextfield子视图,ios,uitableview,addsubview,Ios,Uitableview,Addsubview,我正在试图弄清楚为什么当UITableview滚动时我的子视图会重复。如果我在If语句中添加子视图以重用单元格,它将复制子视图并对其重新排序。如果我把代码放在外面,滚动时会把子视图放在另一个子视图的上面,这会把事情搞砸。下面是我的代码。我做错了什么导致了这一切 static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellI

我正在试图弄清楚为什么当UITableview滚动时我的子视图会重复。如果我在If语句中添加子视图以重用单元格,它将复制子视图并对其重新排序。如果我把代码放在外面,滚动时会把子视图放在另一个子视图的上面,这会把事情搞砸。下面是我的代码。我做错了什么导致了这一切

static NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];





cell.accessoryType = UITableViewCellAccessoryNone;

    UIColor *cellBackgroundColor = [rgbConverter convertColor:@"#202e35"];
    cell.backgroundColor = cellBackgroundColor;
    // cell.contentView.backgroundColor    = color;


    if ((indexPath.row == 0) && (indexPath.section == 0)) {
        fullName = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
        fullName.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        fullName.autocorrectionType = UITextAutocorrectionTypeNo;
        [fullName setClearButtonMode:UITextFieldViewModeWhileEditing];
        fullName.textColor  = [UIColor lightGrayColor];
        [fullName setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        fullName.returnKeyType = UIReturnKeyNext;
        fullName.background = [UIImage imageNamed:@"textfieldBG.png"];
        [fullName setTag:1];
        [fullName setText:@""];

        [cell.contentView  addSubview:fullName];

    }
    if ((indexPath.row == 1)  && (indexPath.section == 0)){
        accountNumber = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
        accountNumber.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        //accountNumber.placeholder = @"MLGW Account Number";
        accountNumber.autocorrectionType = UITextAutocorrectionTypeNo;
        [accountNumber setClearButtonMode:UITextFieldViewModeWhileEditing];
        accountNumber.keyboardType = UIKeyboardTypeNumberPad;
        accountNumber.returnKeyType = UIReturnKeyNext;
        accountNumber.textColor  = [UIColor lightGrayColor];
        [accountNumber setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [accountNumber setTag:2];
        [accountNumber setText:@""];
        accountNumber.background = [UIImage imageNamed:@"textfieldBG.png"];

        [cell.contentView  addSubview:accountNumber];

    }
    if ((indexPath.row == 2)  && (indexPath.section == 0)){
        address = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
        address.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        address.placeholder = @"Address";
        address.autocorrectionType = UITextAutocorrectionTypeNo;
        [address setClearButtonMode:UITextFieldViewModeWhileEditing];
        address.keyboardType = UIKeyboardTypeDefault;
        address.returnKeyType = UIReturnKeyNext;
        address.textColor  = [UIColor lightGrayColor];
        [address setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [address setTag:3];
        [address setText:@""];
        address.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:address];

    }
    if ((indexPath.row == 3)  && (indexPath.section == 0)){
        aptNumber = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
        aptNumber.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        aptNumber.placeholder = @"Apartment Number";
        aptNumber.autocorrectionType = UITextAutocorrectionTypeNo;
        aptNumber.returnKeyType = UIReturnKeyNext;
        [aptNumber setClearButtonMode:UITextFieldViewModeWhileEditing];
        aptNumber.keyboardType = UIKeyboardTypeNamePhonePad;
        aptNumber.textColor  = [UIColor lightGrayColor];
        [aptNumber setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [aptNumber setTag:4];
        [aptNumber setText:@""];
        aptNumber.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:aptNumber];

    }
    if ((indexPath.row == 4)  && (indexPath.section == 0)){
        city = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 180, 25)];
        city.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        city.placeholder = @"City";
        city.autocorrectionType = UITextAutocorrectionTypeNo;
        city.returnKeyType = UIReturnKeyNext;
        [city setClearButtonMode:UITextFieldViewModeWhileEditing];
        city.keyboardType = UIKeyboardTypeDefault;
        city.textColor  = [UIColor lightGrayColor];
        [city setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [city setTag:5];
        [city setText:@""];
        city.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:city];

        zip = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+210, cell.frame.origin.y+10,80, 25)];
        zip.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        zip.placeholder = @"Zip";
        zip.autocorrectionType = UITextAutocorrectionTypeNo;
        zip.returnKeyType = UIReturnKeyNext;
        [zip setClearButtonMode:UITextFieldViewModeWhileEditing];
        zip.keyboardType = UIKeyboardTypeNumberPad;
        zip.textColor  = [UIColor lightGrayColor];
        [zip setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [zip setTag:6];
        [zip setText:@""];
        zip.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:zip];

    }
     if ((indexPath.row == 5)  && (indexPath.section == 0)){
        phoneNbr = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
        phoneNbr.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        phoneNbr.placeholder = @"Phone Number";
        phoneNbr.autocorrectionType = UITextAutocorrectionTypeNo;
        phoneNbr.returnKeyType = UIReturnKeyNext;
        [phoneNbr setClearButtonMode:UITextFieldViewModeWhileEditing];
        phoneNbr.keyboardType = UIKeyboardTypeNumberPad;
        phoneNbr.textColor  = [UIColor lightGrayColor];
        [phoneNbr setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [phoneNbr setTag:7];
        [phoneNbr setText:@""];
        phoneNbr.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:phoneNbr];

    }
    if ((indexPath.row == 6)  && (indexPath.section == 0)){
        emailAddress = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
        emailAddress.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        emailAddress.placeholder = @"Email Address";
        emailAddress.returnKeyType = UIReturnKeyNext;
        emailAddress.autocorrectionType = UITextAutocorrectionTypeNo;
        [emailAddress setClearButtonMode:UITextFieldViewModeWhileEditing];
        emailAddress.keyboardType = UIKeyboardTypeEmailAddress;
        emailAddress.textColor  = [UIColor lightGrayColor];
        [emailAddress setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [emailAddress setTag:8];
        [emailAddress setText:@""];
        emailAddress.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:emailAddress];

    }
    if ((indexPath.row == 7)  && (indexPath.section == 0)){
        validateEmail = [[UITextField alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 200, 25)];
        validateEmail.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        validateEmail.placeholder = @"Re-enter Email Address";
        validateEmail.returnKeyType = UIReturnKeyNext;
        validateEmail.autocorrectionType = UITextAutocorrectionTypeNo;
        [validateEmail setClearButtonMode:UITextFieldViewModeWhileEditing];
        validateEmail.keyboardType = UIKeyboardTypeEmailAddress;
        validateEmail.textColor  = [UIColor lightGrayColor];
        [validateEmail setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [validateEmail setTag:9];
        [validateEmail setText:@""];
        validateEmail.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:validateEmail];

    }
    if ((indexPath.row == 0)  && (indexPath.section == 1)){
         oneTimeDonationBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
        [oneTimeDonationBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
        [oneTimeDonationBtn addTarget:self action:@selector(oneTimeAmountAction:) forControlEvents:UIControlEventTouchUpInside];
        oneTimeDonation = [[UITextField alloc] initWithFrame:CGRectMake(oneTimeDonationBtn.frame.origin.x+35, cell.frame.origin.y+13, 150, 25)];
        oneTimeDonation.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        oneTimeDonation.placeholder = @"One Time Donation";
        oneTimeDonation.autocorrectionType = UITextAutocorrectionTypeNo;
        oneTimeDonation.returnKeyType = UIReturnKeyNext;
        [oneTimeDonation setClearButtonMode:UITextFieldViewModeWhileEditing];
        oneTimeDonation.keyboardType = UIKeyboardTypeDecimalPad;
        oneTimeDonation.textColor  = [UIColor lightGrayColor];
        [oneTimeDonation setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [oneTimeDonation setTag:10];
        [oneTimeDonation setText:@""];
        oneTimeDonation.background = [UIImage imageNamed:@"textfieldBG.png"];
        [cell.contentView  addSubview:oneTimeDonationBtn];
        [cell.contentView  addSubview:oneTimeDonation];

    }
    if ((indexPath.row == 0) && (indexPath.section == 2)){
        oneDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
         [oneDollarBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
        [oneDollarBtn addTarget:self action:@selector(oneDollarAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView  addSubview:oneDollarBtn];

        oneDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(oneDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
        oneDollarLbl.text = @"$1";
        oneDollarLbl.textColor = [UIColor lightGrayColor];
        [cell.contentView  addSubview:oneDollarLbl];

        fiveDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(oneDollarLbl.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
        [fiveDollarBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
        [fiveDollarBtn addTarget:self action:@selector(fiveDollarAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView  addSubview:fiveDollarBtn];

        fiveDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(fiveDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
        fiveDollarLbl.text = @"$5";
        fiveDollarLbl.textColor = [UIColor lightGrayColor];
        [cell.contentView  addSubview:fiveDollarLbl];

        tenDollarBtn = [[UIButton alloc]initWithFrame:CGRectMake(fiveDollarLbl.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
        [tenDollarBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
        [tenDollarBtn addTarget:self action:@selector(tenDollarAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView  addSubview:tenDollarBtn];

        tenDollarLbl = [[UILabel alloc ] initWithFrame:CGRectMake(tenDollarBtn.frame.origin.x+35, cell.frame.origin.y+10, 38, 32)];
        tenDollarLbl.text = @"$10";
        tenDollarLbl.textColor = [UIColor lightGrayColor];
        [cell.contentView  addSubview:tenDollarLbl];
    }

if ((indexPath.row == 1)  && (indexPath.section == 2)){
    otherAmountBtn = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 38, 32)];
    [otherAmountBtn setImage:[UIImage imageNamed:@"radioBox.png"] forState:UIControlStateNormal];
    [otherAmountBtn addTarget:self action:@selector(otherAmountAmountAction:) forControlEvents:UIControlEventTouchUpInside];
        otherAmount = [[UITextField alloc] initWithFrame:CGRectMake(otherAmountBtn.frame.origin.x+35, cell.frame.origin.y+13, 150, 25)];
        otherAmount.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        otherAmount.placeholder = @"Other Amount";
        otherAmount.autocorrectionType = UITextAutocorrectionTypeNo;
        otherAmount.returnKeyType = UIReturnKeyNext;
        [otherAmount setClearButtonMode:UITextFieldViewModeWhileEditing];
        otherAmount.keyboardType = UIKeyboardTypeDecimalPad;
        otherAmount.textColor  = [UIColor lightGrayColor];
        [otherAmount setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        [otherAmount setTag:10];
        [otherAmount setText:@""];
        otherAmount.background = [UIImage imageNamed:@"textfieldBG.png"];
    [cell.contentView  addSubview:otherAmountBtn];
        [cell.contentView  addSubview:otherAmount];

    }


    if ((indexPath.row == 0)  && (indexPath.section == 3)){
        comments = [[UITextView alloc] initWithFrame:CGRectMake(cell.frame.origin.x+10, cell.frame.origin.y+10, 280, 100)];
        comments.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
        comments.autocorrectionType = UITextAutocorrectionTypeNo;
        comments.keyboardType = UIKeyboardTypeDefault;
        comments.textColor  = [UIColor lightGrayColor];
        [comments setTag:11];
        [comments setText:@""];
        comments.contentInset = UIEdgeInsetsMake(2.0, 1.0, 0.0, 0.0);
        [cell.contentView  addSubview:comments];


    }
}


//cell.imageView.image = [UIImage imageNamed:@"settings_icon.png"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];

return cell;
}

当您仅使用一个cellIdentifier通过调用:dequeueReusableCellWithIdentifier方法重用不同类型的单元格时,就会发生这种情况

在您的代码中,您在不同的indexPath中添加了不同的视图,但不知道dequeueReusableCellWithIdentifier方法给出了什么单元格

例如,当您将地址视图添加到重用的单元格时,该单元格上可能已经有一个全名子视图。然后你会得到一个意想不到的结果

有很多方法可以解决这个问题

这里有两个我认为对你的情况来说很容易,你可以选择你喜欢的任何一个:

由于单元格内容的可重用性较差,如果性能没有问题,我建议您根本不要使用dequeueReusableCellWithIdentifier

对于不同类型的单元,您最好使用不同的单元标识符


主要原因是,顾名思义,dequeueReusableCellWithIdentifier获取已创建的单元格,而不考虑其顺序。我猜它只能缓存和可见细胞一样多的细胞

当您向上滚动时,将显示一个已创建并填充的单元格

将单元格创建替换为以下内容:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
同时删除if单元格==nil。同时删除其他单元格创建


请记住,dequeueReusableCellWithIdentifier:forIndexPath:从不返回nil。因此,最好检查单元格是否已填充以跳过子视图的创建。

您的代码有点难理解

每次需要大量自定义单元格时,不应使用UITableViewCell,而应将其子类化,并在自定义类中对其进行自定义

根据我的经验,当节和行都涉及时,我发现if语句很复杂

我建议您使用switch语句,在这里您可以为每种情况提供逻辑

这将使您的生活更轻松,大致如下:

- (UITableViewCell *)tableView:(UITableView *)tableView 
                              cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    //fullName custom cell
                    break;
                case 1:
                    //account number custom cell
                    break;

                default:
                    break;
            }
            break;

        default:
            break;
    }

    return cell;

}

首先将textfield标记设置为某个常量,您可以使用cell indexPath获取文本字段。其次,如果textfield不存在,请添加它,如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *cellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

     //check if you already have textfield, say a constant tag is 100
     UITextField *myTextField ;

    if(cell){

            myTextField = (UITextField *)[cell.contentView viewWithTag:100];
    }

    if (cell == nil) {
               cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    //now if myTextField is nil then only init it
    if(!myTextField){
           //Configure textfield here and add to cell content view
    }


 }

//now all things are configure for cell , assign whatever text you want to myTextField
if(indexPath.row == 0 && indexPath.section == 0){

  //.......
  [myTextField setText:@"XYZ"];



}

}

正如其他人所暗示的那样,如果成功检索到可重用单元,则必须在添加视图之前对其进行清理。您可以在添加新的子视图之前删除以前的子视图。如果有其他循环,请将其放在前面