【问题标题】:Get and set values from a UITextfield in a table view cell从表格视图单元格中的 UITextfield 获取和设置值
【发布时间】:2017-08-01 22:13:22
【问题描述】:

我在单元格中有 4 个文本字段。当我在其中一些输入值时,我会调用一个方法,然后需要在另一个文本字段中显示一些数据。我有 3 行单元格。目前只有最后一行的值是正确的。

    { 
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        cell.qntityTxt.tag=indexPath.row;

        [cell.qntityTxt addTarget:self action:@selector(totalCalc:) forControlEvents:UIControlEventEditingDidEnd];


        return cell;
    }

    - (void)totalCalc:(UITextField*)txtfield
    {
        int total,price,qty;
          NSLog(@"Tag value %ld",(long)txtfield.tag);
            price=[cell.priceTxt.text intValue];
            qty=[cell.qntityTxt.text  intValue];
            total=price*qty;

            Total=[NSString stringWithFormat:@"%d",total];
        cell.totlTxt.text=Total;

        [_addPdtTblVw reloadData];

    }

    [![enter image description here][1]][1]

【问题讨论】:

  • 你是如何在 totalcalc 方法中得到单元格的?
  • 我得到了解决方案 - (void)totalCalc:(UITextField*)txtfield { int total,price,qty; NSLog(@"标签值 %ld",(long)txtfield.tag); for (int i=0; i indexPath = [NSIndexPath indexPathForRow: i inSection: 0];单元格 = [_addPdtTblVw cellForRowAtIndexPath:indexPath]; UITextField *TextProduct= (UITextField)[cell.contentView viewWithTag:8];
  • UITextField TextPrice= (UITextField)[cell.contentView viewWithTag:9]; UITextField TextQuantity= (UITextField)[cell.contentView viewWithTag:10]; UITextField TextTotal= (UITextField)[cell.contentView viewWithTag:11]; NSLog(@"TextProduct mthd :%@",TextProduct.text); NSLog(@"TextPrice mthd :%@",TextPrice.text); NSLog(@"TextQuantity :%@",TextQuantity.text); NSLog(@"TextTotal mthd :%@",TextTotal.text);价格=[TextPrice.text intValue];数量=[TextQuantity.text intValue];总计=价格*数量;
  • Total=[NSString stringWithFormat:@"%d",total]; cell.totlTxt.text=总计; } [_addPdtTblVw reloadData]; }
  • @lj999 请不要在 cmets 中发布代码。 edit您的问题,如果您有更多详细信息要添加或发布实际答案,如果您解决了自己的问题。

标签: ios objective-c uitableview cocoa-touch uitextfield


【解决方案1】:

这和你评论的一样!!

    (void)totalCalc:(UITextField*)txtfield { 
    int total,price,qty;
     NSLog(@"Tag value %ld",(long)txtfield.tag); 
    for (int i=0; i < 3; i++){ 
    NSIndexPath indexPath = [NSIndexPath indexPathForRow: i inSection: 0];
    cell = [_addPdtTblVw cellForRowAtIndexPath:indexPath];
    UITextField *TextProduct= (UITextField)[cell.contentView viewWithTag:8];
    UITextField *TextPrice= (UITextField)[cell.contentView viewWithTag:9];
    UITextField *TextQuantity= (UITextField)[cell.contentView viewWithTag:10];
    UITextField *TextTotal= (UITextField)[cell.contentView viewWithTag:11];
    price=[TextPrice.text intValue];
    qty=[TextQuantity.text intValue];
    total=price*qty;
    Total=[NSString stringWithFormat:@"%d",total];
    cell.totlTxt.text=Total; 
    }
    [_addPdtTblVw reloadData];
    } 

【讨论】:

  • 由于未捕获的异常“NSInvalidArgumentException”,我得到了这个异常终止应用程序,原因:“-[UIView priceTxt]:无法识别的选择器发送到实例 0x7fb862d0d9e0”
  • 你有没有解决方案??你在评论中写了一些东西!
  • 是的,我自己找到了解决方案。当我输入你的代码时,它给出了异常
  • 你想要我的解决方案然后我会努力吗?你的愿望:)
  • 是的,我知道.. 等一下!正在努力!你想更新所有单元格的所有文本字段吗??
猜你喜欢
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多