如何在UITableViewCell中拉取UITextField的文本值?

时间:2011-02-28 19:42:29

标签: cocoa-touch ipad uitableview uitextfield

我正在尝试使用UITableView设置基本排序。单元格的数量取决于通过segmentedControl选择的字母,而segmentedControl又会将单元格重新加载到新产品中。

我遇到问题的部分是访问每个产品的数量。每个UITableViewCell都有一个产品图像和几个标签以及UITextField和一个动作按钮。这是我创建单元格的代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UIButton *productButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *productButtonImage = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:[[productList objectAtIndex:indexPath.row] valueForKey:@"product_small_image_filepath"] options:NSDataReadingMapped error:nil]] autorelease];
        productButton.frame = CGRectMake(9.0, 3.0, 48.0, 84.0);
        [productButton setBackgroundImage:productButtonImage forState:UIControlStateNormal];
        [productButton setTag:indexPath.row];
        [productButton addTarget:self action:@selector(loadProductDetailAtIndexPath:) forControlEvents:UIControlEventTouchDown];

        UILabel *productCodeLabel = [[[UILabel alloc] initWithFrame:CGRectMake(67.0, 0.0, 300.0, 34.0)] autorelease];
        productCodeLabel.tag = 100;
        [productCodeLabel setBackgroundColor:[UIColor clearColor]];
        [productCodeLabel setTextColor:[UIColor whiteColor]];

        UILabel *productNameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(67.0, 34.0, 300.0, 23.0)] autorelease];
        productNameLabel.tag = 101;
        [productNameLabel setBackgroundColor:[UIColor clearColor]];
        [productNameLabel setTextColor:[UIColor lightGrayColor]];

        UILabel *productSizeLabel = [[[UILabel alloc] initWithFrame:CGRectMake(67.0, 57.0, 300.0, 23.0)] autorelease];
        productSizeLabel.tag = 102;
        [productSizeLabel setBackgroundColor:[UIColor clearColor]];
        [productSizeLabel setTextColor:[UIColor grayColor]];

        UILabel *typeQuantityLabel = [[[UILabel alloc] initWithFrame:CGRectMake(380.0, 35.0, 100.0, 30.0)] autorelease];
        typeQuantityLabel.tag = 103;
        [typeQuantityLabel setBackgroundColor:[UIColor clearColor]];
        [typeQuantityLabel setTextColor:[UIColor whiteColor]];

        UITextField *numberOfItemsTextField = [[[UITextField alloc] initWithFrame:CGRectMake(480.0, 35.0, 150.0, 30.0)] autorelease];
        numberOfItemsTextField.tag = 104;
        [numberOfItemsTextField setKeyboardType:UIKeyboardTypeNumberPad];
        [numberOfItemsTextField setReturnKeyType:UIReturnKeyDone];
        [numberOfItemsTextField setBackgroundColor:[UIColor clearColor]];
        [numberOfItemsTextField setBorderStyle:UITextBorderStyleRoundedRect];
        [numberOfItemsTextField setTextAlignment:UITextAlignmentRight];

        UIButton *productAddButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        productAddButton.frame = CGRectMake(650.0, 35.0, 70.0, 30.0);
        productAddButton.tag = indexPath.row;
        [productAddButton setBackgroundColor:[UIColor clearColor]];
        [productAddButton setTitle:@"ADD" forState:UIControlStateNormal];
        [productAddButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [productAddButton addTarget:self action:@selector(addItemToOrderedItemsMutableArray:) forControlEvents:UIControlEventTouchDown];

        [cell addSubview:productButton];
        [cell addSubview:productCodeLabel];
        [cell addSubview:productNameLabel];
        [cell addSubview:productSizeLabel];
        [cell addSubview:typeQuantityLabel];
        [cell addSubview:numberOfItemsTextField];
        [cell addSubview:productAddButton];

        UIView *v = [[[UIView alloc] init] autorelease];
        v.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.5];
        [cell setSelectedBackgroundView:v];
    } 
    // Configure the cell...
    UIButton *productButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *productButtonImage = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:[[productList objectAtIndex:indexPath.row] valueForKey:@"product_small_image_filepath"] options:NSDataReadingMapped error:nil]] autorelease];
    productButton.frame = CGRectMake(9.0, 3.0, 48.0, 84.0);
    [productButton setBackgroundImage:productButtonImage forState:UIControlStateNormal];
    [productButton setTag:indexPath.row];
    [productButton addTarget:self action:@selector(loadProductDetailAtIndexPath:) forControlEvents:UIControlEventTouchDown];
    [cell addSubview:productButton];

    UILabel *productCodeLabel = (UILabel *)[cell viewWithTag:100];
    [productCodeLabel setText:[[productList objectAtIndex:indexPath.row] valueForKey:@"product_code"]];
    self.productCode = [[productList objectAtIndex:indexPath.row] valueForKey:@"product_code"];

    UILabel *productNameLabel = (UILabel *)[cell viewWithTag:101];
    [productNameLabel setText:[[productList objectAtIndex:indexPath.row] valueForKey:@"product_name"]];

    UILabel *productSizeLabel = (UILabel *)[cell viewWithTag:102];
    [productSizeLabel setText:[[productList objectAtIndex:indexPath.row] valueForKey:@"product_size"]];

    UILabel *typeQuantityLabel = (UILabel *)[cell viewWithTag:103];
    [typeQuantityLabel setText:@"QUANTITY"];

    UITextField *quantityTextField = (UITextField *)[cell viewWithTag:104];
    [quantityTextField setText:@"0"];
    productQuantityTextField = quantityTextField;

    return cell;
}

所有信息都在设备上呈现,但当输入单个产品的数量时,quantityTextField仅分配给屏幕上的最后一个Cell。我的问题是:如何将指针移动到UITable中的前一个单元格,以便能够获得给定产品的值?

2 个答案:

答案 0 :(得分:2)

我在上面的方法中看到,您将此方法所在类中的productQuantityTextField分配给来自单元格的文本字段。正如您所发现的,每次新的单元格在屏幕上变得可见时指针会发生变化,并且无法找回其他指针。您可能已经注意到productQuantityTextField不一定与页面中的最后一个文本字段相对应。

在这种情况下,最好的办法是让您的班级实施UITextFieldDelegate,并将您的班级指定为您创建的每个数量文本字段的代表。在textFieldDidEndEditing:中,您将确定特定文本字段的相应产品,并将文本值保存到该特定产品。您还需要更改上面的代码以回读该数量并将其设置为quantityTextField而不是“0”,否则您会发现滚动屏幕上的某个部分然后重新打开使它似乎忘记了数量

答案 1 :(得分:1)

理想情况下,您可以将其中一些重构为自定义UITableViewCell类。从长远来看,维护起来会容易得多。

您不应该依赖您的单元格或UITextField作为数据的后备存储。而是让UITextField的委托在文本更改时更新您的数据模型。您的单元格可以根据需要缓存指向数据模型的指针。在将文本字段放入单元格时,这一点尤为重要,因为您无法管理单元格的生命周期。也就是说,如果它在屏幕外滚动,您可能无法访问给定单元格,因为它已被解除分配或重新使用。