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?