TableViewCell中的UItextField包含多个部分 - 数据重复

时间:2017-05-20 09:23:39

标签: ios objective-c uitableview uitextfield tableviewcell

我有一个tableview来复制表单页面。它有两个部分,第0部分有8个单元,其他部分有4个单元

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [tableViewSectionData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section)
    {
        case 0:
        return 8;
            break;
        case 1:
        return 4;
            break;
        default:
        return 0;
            break;
    }   
}

一些单元格用于显示选择器,我通过在单元格内的文本字段上添加一个按钮来完成,然后在必要时启用它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        TextFieldCell *cellInSection0 = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCellInSection0" forIndexPath:indexPath];

        if (!cellInSection0)
        {
            cellInSection0 = [[TextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextFieldCellInSection0"];
        }

        cellInSection0.bottomButton.hidden = YES;

        cellInSection0.textFieldBottomLabel.alpha = 0.5;
        cellInSection0.textFieldImageView.alpha = 0.5;

        cellInSection0.textfield.delegate = self;
        cellInSection0.textfield.tag = indexPath.row;

        cellInSection0.pickerViewButton.hidden = YES;

        NSString *placeholderString;
        NSString *imageName;

        switch (indexPath.row)
        {
            case 0:
            {
                if (fname)
                {
                    cellInSection0.textfield.text = fname;
                    cellInSection0.textFieldBottomLabel.alpha = 1.0;
                    cellInSection0.textFieldImageView.alpha = 1.0;
                }
                else
                {
                    placeholderString = @"FIRST NAME";
                }
                imageName = @"user";
            }
                break;
            case 1:
            {
                if (lname)
                {
                    cellInSection0.textfield.text = lname;
                    cellInSection0.textFieldBottomLabel.alpha = 1.0;
                    cellInSection0.textFieldImageView.alpha = 1.0;
                }
                else
                {
                    placeholderString = @"LAST NAME";
                }
                imageName = @"user";
            }
                break;
            case 2:
            {
                if (address)
                {
                    cellInSection0.textfield.text = address;
                    cellInSection0.textFieldBottomLabel.alpha = 1.0;
                    cellInSection0.textFieldImageView.alpha = 1.0;
                }
                else
                {
                    placeholderString = @"ADDRESS";
                }
                imageName = @"address";
            }
                break;
            case 3:
            {
                cellInSection0.textfield.returnKeyType = UIReturnKeyDone;

                if (floor_unit)
                {
                    cellInSection0.textfield.text = floor_unit;
                    cellInSection0.textFieldBottomLabel.alpha = 1.0;
                    cellInSection0.textFieldImageView.alpha = 1.0;
                }
                else
                {
                    placeholderString = @"FLOOR/UNIT";
                }
                imageName = @"address";
            }
                break;
            case 4:
            {
                cellInSection0.pickerViewButton.hidden = NO;
                cellInSection0.textfield.enabled = NO;

                cellInSection0.pickerViewButton.tag = indexPath.row;

                [cellInSection0.pickerViewButton addTarget:self action:@selector(showCountryPicker:) forControlEvents:UIControlEventTouchUpInside];

                if (countryName)
                {
                    cellInSection0.textfield.text = countryName;
                    cellInSection0.textFieldBottomLabel.alpha = 1.0;
                    cellInSection0.textFieldImageView.alpha = 1.0;
                    cellInSection0.textfield.alpha = 1.0;
                }
                else
                {
                    cellInSection0.textfield.text = @"COUNTRY";
                    cellInSection0.textfield.alpha = 0.5;
                }
                imageName = @"mapicon";
            }
                break;
            case 5:
            {
                cellInSection0.pickerViewButton.hidden = NO;
                cellInSection0.textfield.enabled = NO;

                cellInSection0.pickerViewButton.tag = indexPath.row;

                [cellInSection0.pickerViewButton addTarget:self action:@selector(showStatePicker:) forControlEvents:UIControlEventTouchUpInside];

                if (stateName)
                {
                    cellInSection0.textfield.text = stateName;
                    cellInSection0.textFieldBottomLabel.alpha = 1.0;
                    cellInSection0.textFieldImageView.alpha = 1.0;
                    cellInSection0.textfield.alpha = 1.0;
                }
                else
                {
                    cellInSection0.textfield.text = @"STATE";
                    cellInSection0.textfield.alpha = 0.5;
                }
                imageName = @"mapicon";
            }
                break;
            case 6:
            {
                cellInSection0.pickerViewButton.hidden = NO;
                cellInSection0.textfield.enabled = NO;

                cellInSection0.pickerViewButton.tag = indexPath.row;

                [cellInSection0.pickerViewButton addTarget:self action:@selector(showCityPicker:) forControlEvents:UIControlEventTouchUpInside];

                if (cityName)
                {
                    cellInSection0.textfield.text = cityName;
                    cellInSection0.textFieldBottomLabel.alpha = 1.0;
                    cellInSection0.textFieldImageView.alpha = 1.0;
                    cellInSection0.textfield.alpha = 1.0;
                }
                else
                {
                    cellInSection0.textfield.text = @"CITY";
                    cellInSection0.textfield.alpha = 0.5;
                }
                imageName = @"mapicon";
            }
                break;
            case 7:
            {
                placeholderString = @"ZIP";
                imageName = @"mapicon";
            }
                break;
            default:
                break;
        }

        if (placeholderString)
        {
            NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:placeholderString attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];

            cellInSection0.textfield.attributedPlaceholder = placeHolder;
        }

        cellInSection0.textFieldImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", imageName]];

        return cellInSection0;
    }
    else
    {
        TextFieldCell *cellInSection1 = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCellInSection1" forIndexPath:indexPath];

        if (!cellInSection1)
        {
            cellInSection1 = [[TextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextFieldCellInSection1"];
        }

        cellInSection1.bottomButton.hidden = YES;

        cellInSection1.textFieldBottomLabel.alpha = 0.5;
        cellInSection1.textFieldImageView.alpha = 0.5;

        cellInSection1.textfield.delegate = self;
        cellInSection1.textfield.tag = indexPath.row;

        NSString *placeholderString;
        NSString *imageName;

        switch (indexPath.row)
        {
            case 0:
            {
                if (cardNumber)
                {
                    cellInSection1.textfield.text = cardNumber;
                }
                else
                {
                    placeholderString = @"CARD NO.";
                }
                imageName = @"yearofincorporationicon";
            }
                break;
            case 1:
            {
                if (month)
                {
                    cellInSection1.textfield.text = month;
                }
                else
                {
                    placeholderString = @"MONTH";
                }
                imageName = @"calender";
            }
                break;
            case 2:
            {
                if (year)
                {
                    cellInSection1.textfield.text = year;
                }
                else
                {
                    placeholderString = @"YEAR";
                }

                imageName = @"calender";
            }
                break;
            case 3:
            {
                if (cvv)
                {
                    cellInSection1.textfield.text = cvv;
                }
                else
                {
                    placeholderString = @"CVV";
                }
                imageName = @"yearofincorporationicon";
            }
                break;
            default:
                break;
        }

        if (placeholderString)
        {
            NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:placeholderString attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];

            cellInSection1.textfield.attributedPlaceholder = placeHolder;
        }

        cellInSection1.textFieldImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", imageName]];

        return cellInSection1;
    }
}

乍一看这很好用。但是当我在文本字段中输入一些文本时(比如第0部分中的文本字段),第1部分的文本字段也显示相同的文本。这发生在每个文本字段中。有时占位符字符串也不匹配。它发生得如此随机,我无法解决问题所在。

任何帮助都很棒。 感谢

2 个答案:

答案 0 :(得分:0)

您需要创建跟踪文本字段值的单独值。并且viewDidLoad中的初始值更改

 var arrInitialValues: [Any] = ["", "", ""] // this array count should be same as datasource array count

在cellForRowAtIndexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  cell.txtfield.text = arrInitialValues[indexPath.row] as! String
}

在委托方法中替换该值。

func textFieldDidEndEditing(_ textField: UITextField) {

    let buttonPosition = textField.convertPoint(CGPoint.zero, to: tblView)
    let indexPath: IndexPath? = tblView.indexPathForRow(at: buttonPosition)
    arrInitialValues[indexPath?.row] = textField.text  // or if you use model property then you can change that property here
}

请注意,这是您可以根据需要修改的示例代码

答案 1 :(得分:0)

这是因为细胞被重复使用。如果您没有正确设置if-else条件中的属性值,这是一个常见问题。

例如,在您的第一个switch案例if条件中,您正在更改textField属性,但不会更改其他条件。

让我们说满足第一个小区if条件。现在让我们说当重复使用该单元时,它将进入其他条件。但是由于该单元被重用,它将反映在if条件下对第一个单元所做的更改。由于else条件未更改textField属性,因此您将获得旧值。

我正在更改case 0 if-else条件。

    if (fname)
    {
         cellInSection0.textfield.text = fname;
         cellInSection0.textFieldBottomLabel.alpha = 1.0;
         cellInSection0.textFieldImageView.alpha = 1.0;
         placeholderString = @"ifPartPlaceHolderName";
    }
    else
    {
         cellInSection0.textfield.text = @"Else  part name";
         cellInSection0.textFieldBottomLabel.alpha = elsePartAlphaValue;
         cellInSection0.textFieldImageView.alpha = elsePartAlphaValue;
         placeholderString = @"FIRST NAME";
    }

您还需要更改剩余的案例。请记住,无论您在if条件下更改了哪个单元格属性,都必须在else部分中提供替代方案。