【问题标题】:update array when the user updates the text field in the cell当用户更新单元格中的文本字段时更新数组
【发布时间】:2023-04-03 19:46:01
【问题描述】:

我想知道当用户更新自定义uitableviewcell 中的文本字段时,是否有办法更新barcodeItemsQuantity 数组。下面是我的代码 sn-ps。并且我想在用户从自定义 tableviewcell 更改文本字段时更新我的​​数组中的数据。

viewcontroller.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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

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

    第 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 替换为您想要的模型变量。

    希望这对你有用。

    【讨论】:

    • 谢谢,这对我很有帮助。
    • 欢迎您@jordanzee,如有任何问题,请发表评论,不要忘记为答案投票。
    猜你喜欢
    • 1970-01-01
    • 2014-08-07
    • 2023-03-06
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多