滚动时清除单元格上的 UITextView

UITextView on cell clears when scrolling

各位程序员大家好,

我真的绞尽脑汁想弄清楚问题出在哪里,但我似乎无法弄清楚问题所在。我已经对此进行了研究,但不幸的是对我没有任何帮助。问题是我有多个单元格,每个单元格都有一个特定的 UITextField,我以编程方式在 cellForRowAtIndexPath 上绘制它。问题是,当我在任何文本字段上输入一些信息并在 table 视图上向下滚动时,文本字段上的文本消失得无影无踪。我试图理解为什么单元重用(我认为是导致问题的原因)导致了这个问题。

这是我的 cellForRowAtIndexPath 代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }

    cellText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 260, 20)];
    cellText.font = [UIFont boldSystemFontOfSize:11.0];
    cellText.textColor = [UIColor grayColor];
    [cell.contentView addSubview:cellText];

    if (indexPath.section == 0)
    {
        cellText.text = clientInfoArray[indexPath.row];

        if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:@"Customer Name"])
        {
            nameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            nameTxtField.placeholder = @"full name";
            nameTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            nameTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            [nameTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            [cell addSubview:nameTxtField];
        }
        if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:@"Phone"])
        {
            phoneTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            phoneTxtField.placeholder = @"xxx-xxx-xxx";
            phoneTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            phoneTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            phoneTxtField.keyboardType = UIKeyboardTypePhonePad;
            [phoneTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            [cell addSubview:phoneTxtField];
        }
        if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:@"Date"])
        {
            dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            dateLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:dateLabel];
            dateLabel.text = [dateFormatter stringFromDate:[NSDate date]];
        }
    }
    if (indexPath.section == 1) {

        cellText.text = vehicleInfoArray[indexPath.row];

        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Make and Model"])
        {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            modelLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:modelLabel];
            modelLabel.text = makeAndModelData;

        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Color"])
        {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            colorLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:colorLabel];
            colorLabel.text = colorData;
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Doors"])
        {
            doorsTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            doorsTxtField.placeholder = @"#";
            doorsTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            doorsTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            [doorsTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            doorsTxtField.keyboardType = UIKeyboardTypeNumberPad;
            [cell addSubview:doorsTxtField];
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Vehicle VIN"])
        {
            vinTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            vinTxtField.placeholder = @"#";
            vinTxtField.font = [UIFont boldSystemFontOfSize:15.0];
            vinTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
            [vinTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
            vinTxtField.keyboardType = UIKeyboardTypeNumberPad;
            [cell addSubview:vinTxtField];
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Fuel"])
        {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            fuelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            fuelLabel.font = [UIFont boldSystemFontOfSize:15.0];
            [cell.contentView addSubview:fuelLabel];
            fuelLabel.text = fuelData;
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Milage"])
        {
            milageLabel = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
            milageLabel.placeholder = @"ODO";
            milageLabel.font = [UIFont boldSystemFontOfSize:15.0];
            milageLabel.autocorrectionType = UITextAutocorrectionTypeNo;
            [milageLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
            milageLabel.keyboardType = UIKeyboardTypeNumberPad;
            [cell addSubview:milageLabel];
        }
        if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:@"Other description"])
        {
            otherDTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, 300, 125)];
            otherDTxtView.font = [UIFont boldSystemFontOfSize:15.0];
            [cell addSubview:otherDTxtView];
        }
    }
    if (indexPath.section == 2) {

        cellText.text = InAndOutInfoArray[indexPath.row];
    }
    return cell;
}

关于如何解决这个问题有什么想法吗? + 为有用的答案加分。 :)

每当您使用重用单元格技术时,不可见的单元格将被释放以释放一些内存。

由于您是在 cellForRow 方法中创建文本字段,因此当单元格不可见时它也会被释放,当它变得可见时会重新绘制。

您可以将文本字段值保存在某个数组中(使用文本字段的 didEndEditing 委托)并在 cellForRow 方法内的文本字段创建过程中使用该数组填充文本。