我有一个包含单元格的表格,每个单元格中都有一个 UITextField。我将这些文本字段的委托设置为 self 并在编辑结束时进行一些计算。
我的问题是文本字段,每当我输入第一个字段以外的字段时,除第一个字段外的所有文本字段都会更新。当我输入第一个时,其他的会完美更新。
这让我检查了哪些数据得到了更新,虽然我设置cell.textLabel.text为等于数组中的特定位置,但它没有显示该位置的值。
这是我的cellForRowAtIndex方法:
- (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;
}
当我尝试这个时,经过计算,这是记录的内容:
索引 0 处的值是 1.20
索引 1 处的值是 1.00
索引 2 处的值是 4.55
然而,第一个文本字段仍然显示 0 而不是 1.20
添加这些文本字段我哪里出错了?