lj999 lj999 - 9 months ago 25
iOS Question

Get and set values from a UITextfield in a table view cell

I have 4 text fields in cells. When I enter a value in some of them, I will call a method and then need to display some data in another text field. I have 3 rows of cells. Currently only the values for the last row are coming correctly.

{
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]

Answer Source

This is same as what you commented!!

    (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];
    }