如何拉取UITableViewCell中的UITextField的文本值? [英] How to pull the text value for a UITextField inside a UITableViewCell?

查看:99
本文介绍了如何拉取UITableViewCell中的UITextField的文本值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用UITableView设置一个基本的排序。单元格数量根据通过segmentedControl选择的字母而有所不同,因此会将单元格重新加载到新产品中。

I am trying to set up a basic ordering using a UITableView. The number of cells varies depending on a letter selected through a segmentedControl which in turn reloads the cells to the new products accordingly.

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

The portion I am having the problem with is accessing the quantity for each product. Each UITableViewCell has an image for the product and a couple of labels along with the UITextField and an action button. Here is the code for my cell creation:

- (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中的以前的单元格,以便能够获得给定产品的值?

All the information renders right on the device, but when it comes down to enter the quantity for an individual product, quantityTextField is only assigned to the last Cell on the screen. My question is: How can I move that pointer to the previous cells in the UITable in order to be able to obtain the value for a given product?

推荐答案

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

I see in the method above you assign productQuantityTextField, which is presumably an ivar in the class this method is in, to the text field from the cell. As you've found, each time a new cell becomes visible on screen that pointer gets changed, and there is no way to get back the other pointers. You may or may not have noticed that productQuantityTextField doesn't necessarily even correspond to the last text field in the page.

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

The best thing to do in this case would be to have your class there implement UITextFieldDelegate, and assign your class as the delegate for each quantity text field you create. In textFieldDidEndEditing:, you would determine the appropriate product for the particular text field and save the text value to that particular product. You'd also want to change your code above to read back that quantity and set it to quantityTextField instead of "0" always, otherwise you'll find that scrolling a part off the screen and then back on makes it seem to forget the quantity.

这篇关于如何拉取UITableViewCell中的UITextField的文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆