0
votes

I have a table with cells and in each cell is a UITextField. I have these textfields' delegate set to self and doing some calculations upon editing ended.

My problem is with the textfields, whenever I type in a field other than the first one, all my textfields except the first one gets updated. When I type in the first one, the others update perfectly.

This brought me to check what data gets updated and although I set cell.textLabel.text equal to the specific position in the array, it does not show the value at that position.

Here is my cellForRowAtIndex method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UITextField *tf;

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        tf = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 + 25,
                                                           5,
                                                           cell.contentView.frame.size.width/2 - 25 - 25,
                                                           cell.contentView.frame.size.height - 10)];
        tf.font = [UIFont fontWithName:@"Helvetica" size:16];
        tf.textAlignment = NSTextAlignmentLeft;
        tf.backgroundColor = [UIColor clearColor];
        tf.textColor = [UIColor blueColor];
        tf.tag = indexPath.row;
        tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        tf.returnKeyType = UIReturnKeyDone;
        tf.delegate = self;
        tf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        cell.textLabel.text = [_titles objectAtIndex:indexPath.row];
        tf.placeholder = cell.textLabel.text;

        [cell.contentView addSubview:tf];
    }
    else
    {
        tf = (UITextField *)[cell viewWithTag:indexPath.row];
    }

    tf.text = [NSString stringWithFormat:@"%.2f", [[_data objectAtIndex:indexPath.row] floatValue]];
    NSLog(@"Value at index %i is %.2f", indexPath.row, [[_data objectAtIndex:indexPath.row] floatValue]);

    cell.textLabel.text = [_titles objectAtIndex:indexPath.row];

    return cell;
}

When I tried this, after my calculations, this is what was logged:

Value at index 0 is 1.20
Value at index 1 is 1.00
Value at index 2 is 4.55

The first textfield however still showed 0 instead of 1.20

Where am I going wrong with adding these textfields?

2

2 Answers

0
votes

Every time you are creating textfield and putting it in same place... So your newer textfield is created on back of previoulsy created one.

You need to check this statement

UITextField *tf;

and

tf = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 + 25, 5,
                                                       cell.contentView.frame.size.width/2 - 25 - 25,
                                                       cell.contentView.frame.size.height - 10)];

alloc+init only when previous one is not there. Similar to what you do for cell.

EDIT

Check the tags. If i'm not mistaking, the default tag is 0 and thus when using viewWithTag it might select the textLabel instead of the textfield. Set the textfield's tag to indexPath.row + 5.

0
votes

This line of code look buggy :

tf = (UITextField *)[cell viewWithTag:indexPath.row];

Because if you are reusing a TableViewCell, the row won't match.
e.g. The cell get created as row 0 and get reuse at row 10. So tf will be nil and won't update.

Each cell is its own little ecosystem, so you don't need different tag in every cell for your textfield. it could just be tf.tag = SOME_CONSTANT;

Also I assume that your are asking the table view to reload it's data.