如何在UITableViewCell中为UITextField提供文本值?

问题描述:

我想使用UITableView设置一个基本的顺序。单元的数量根据通过分段控件选择的字母而变化,分段控件又将单元重新加载到新产品。如何在UITableViewCell中为UITextField提供文本值?

我遇到问题的部分是访问每个产品的数量。每个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只分配给最后一个单元格在屏幕上。我的问题是:如何将指针移动到UITable中的前一个单元格以便能够获取给定产品的值?

我在上面的方法中看到您将productQuantityTextField(可能是此方法所在类中的ivar)分配给单元格中的文本字段。正如你发现的那样,每当一个新的单元格在屏幕上变得可见时,指针被改变,并且没有办法找回其他指针。您可能注意到,也可能没有注意到productQuantityTextField甚至不一定对应于页面中的最后一个文本字段。

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

+0

这个选项是我认为最适合这种情况的选项。使用委托是为了避免子类的一切。我没有设置我的类来实现UITextFieldDelegate,而不是textFieldDidEndEditing :,我实现了textFieldShouldReturn:这样我就可以添加需要交互的使用实例方法,例如UITextField的resignFirstResponder。我仍然想念一些我认为的东西; productQuantity值显示在多个单元格上,出于某种原因,上下滚动时产品代码将丢失。它之前没有。 – 2011-03-01 05:12:59

+0

我知道productCode正在为该行保留,因为我可以上下滚动并仍然将任何产品展开到其详细视图。但对于textFields,productCode的值表现出奇怪的不一致的方式。 – 2011-03-01 05:15:59

+0

得到它的工作,感谢您的答案。我甚至删除了添加按钮,因为可以使用 - (BOOL)textFieldShouldReturn:(UITextField *)textField委托方法设置操作。 – 2011-03-03 20:24:42

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

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