首页 > 解决方案 > 当用户更新单元格中的文本字段时更新数组

问题描述

我想知道barcodeItemsQuantity当用户更新 custom 中的文本字段时是否有更新数组的方法uitableviewcell。下面是我的代码片段。每当用户从自定义 tableviewcell 更改文本字段时,我想更新我的数组中的数据。

视图控制器.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Cell Initialized");
    static NSString *cellIdentifier = @"BarcodeItemsCell";

    BarcodeItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (cell == nil) {
        cell = [[BarcodeItemsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Configure the cell...

    if (indexPath.row == [barcodeItems count]) {
        // Add new row
        cell.barcodeLabel.text = @"scan SKU";
        cell.barcodeLabel.textColor = [UIColor lightGrayColor];
        UIImage *btnImage = [UIImage imageNamed:@"barcodeIcon"];
        [cell.leftButton setImage:btnImage forState:UIControlStateNormal];
        cell.leftButton.tintColor = [UIColor blackColor];
        cell.quantityTextField.userInteractionEnabled = NO;
        [cell.leftButton addTarget:self action:@selector(scanBarcode) forControlEvents:UIControlEventTouchUpInside];
        NSLog(@"Add another Item Requested");
    }
    else {
        // Display barcode items
        cell.barcodeLabel.text = [barcodeItems objectAtIndex:indexPath.row];
        UIImage *btnImage = [UIImage imageNamed:@"deleteIcon"];
        cell.leftButton.tintColor = [UIColor redColor];
        [cell.leftButton setImage:btnImage forState:UIControlStateNormal];
        cell.leftButton.tag = indexPath.row;
        [cell.leftButton addTarget:self action:@selector(deleteRow:) forControlEvents:UIControlEventTouchUpInside];
        [barcodeItemsQuantity addObject:cell.quantityTextField];
    }

    NSLog(@"Cell Populated");
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return true;
}

-(void) deleteRow:(id)sender {
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:photoCaptureView.itemsTableView];
    NSIndexPath *indexPath = [photoCaptureView.itemsTableView indexPathForRowAtPoint:buttonPosition];
    [barcodeItems removeObjectAtIndex:indexPath.row];
    [photoCaptureView.itemsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    NSLog(@"Item Removed");
}

barcodeitestableviewcell.h

@interface BarcodeItemsTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIButton *leftButton;
@property (strong, nonatomic) IBOutlet UILabel *barcodeLabel;
@property (strong, nonatomic) IBOutlet UITextField *quantityTextField;


@end

标签: iosobjective-cuitableview

解决方案


是的,您可以更新放置在自定义表格单元格内的文本字段文本的数据模型值。

步骤1:

在当前类“UITextFieldDelegate”中添加委托

class ViewController: UIViewController,UITextFieldDelegate

第2步:

在您的“cellForRowAt indexPath”上调用当前文本字段的委托并添加标签

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    // add the following lines
 cell.textField.delegate = self
 cell.textField.tag = indexPath.row
    return cell
}

第 3 步:

调用 UITextField 代表

func textFieldDidEndEditing(textField: UITextField) {
if !textField.text.isEmpty {    // check textfield contains value or not
    if textField.tag == 0 {
        firstName = textField.text!
    } else {
        lastName = textField.text!
    }
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

func textFieldDidBeginEditing(textField: UITextField!) {
    if textField.tag == 0 {
        firstName = ""
    } else {
        lastName = ""
    }
}

在这里,您可以将 firstName 和 lastName 替换为您想要的模型变量。

希望这对你有用。


推荐阅读