滚动

时间:2015-11-13 02:18:36

标签: objective-c uitableview uitextfield cells

Hello其他程序员,

我真的很想知道问题是什么,但我似乎无法弄清楚问题。我已经对此进行过研究,但不幸的是,对我来说没有任何作用。问题是我有多个单元格,每个单元格都有一个特定的UITextField,我以编程方式在cellForRowAtIndexPath上绘制。问题是,当我在任何文本字段上输入一些信息并向下滚动表格视图时,文本字段上的文本消失得无影无踪。我试图理解为什么细胞再利用(我认为导致问题)导致了这一点。

这是我的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;
}

有关如何解决此问题的任何想法? +额外积分可获得有用的答案。 :)

1 个答案:

答案 0 :(得分:1)

无论何时使用重用单元技术,都会释放不可见的单元以释放一些内存。

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

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