0

I have a tableview which has 3 rows in it, each row having a label and a textfield. The code is as follows:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [[NSArray alloc] initWithObjects:@"Mortgage Balance", @"Interest Rate", @"Term (yrs)", nil];
    self.fieldLabels = array;
    [array release];

    static NSString *CellIdentifier = @"Cell";
    NSUInteger row = [indexPath row];
    UILabel *label;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 125, 25)];
        label.textAlignment = UITextAlignmentRight;
        label.tag = kLabelTag;
        label.font = [UIFont boldSystemFontOfSize:18];
        [cell.contentView addSubview:label];
        [label release];
        UITextField *textField = [[UITextField alloc] initWithFrame:
                                  CGRectMake(170, 12, 250, 25)];
        textField.clearsOnBeginEditing = NO;
        [textField setDelegate:self];
        [textField addTarget:self 
                      action:@selector(textFieldDone:) 
            forControlEvents:UIControlEventEditingDidEndOnExit];
        [cell.contentView addSubview:textField];
    }
    label = (UILabel *)[cell viewWithTag:kLabelTag];
    UITextField *textField = nil;
    for (UIView *oneView in cell.contentView.subviews)
    {
        if ([oneView isMemberOfClass:[UITextField class]])
            textField = (UITextField *)oneView;
    }
    label.text = [fieldLabels objectAtIndex:row];
    label.font = [UIFont boldSystemFontOfSize:14];
    textField.tag = row; 

    switch (row) {
        case 0:
            self.balance = textField;
            self.balance.clearsOnBeginEditing = NO;
            self.balance.text = @"test";
            [balance addTarget:self 
                        action:@selector(balanceDone:) 
              forControlEvents:UIControlEventEditingDidEndOnExit];
            [cell.contentView addSubview:balance];
            break;
        case 1:
            self.interestrate = textField;
            self.interestrate.clearsOnBeginEditing = NO;
            self.interestrate.text = @"5.5";
            [interestrate addTarget:self 
                             action:@selector(interestrateDone:) 
                   forControlEvents:UIControlEventEditingDidEndOnExit];
            [cell.contentView addSubview:interestrate];
            break;
        case 2:
            self.term = textField;
            self.term.clearsOnBeginEditing = NO;
            self.term.text = @"30";
            self.term.delegate = self;
            [term addTarget:self 
                     action:@selector(termDone:) 
           forControlEvents:UIControlEventEditingDidEndOnExit];
            [cell.contentView addSubview:balance];
            break;
    }
    // Configure the cell.
    return cell;
}

- (void)interestrateDone:(id)sender {
    mortgage_interest = [interestrate.text doubleValue]; 
}

- (void)balanceDone:(id)sender {
    mortgage_balance = [balance.text doubleValue];
}

- (void)termDone:(id)sender {
    mortgage_term = [term.text intValue];
}

When I change this to 2 rows, the 1st and 2nd rows are displayed and I can enter values in each of the textfields. When I change this to 3 rows, I can't select the 1st row's textfield. In fact, I added the text to load into each field, and the second row has the correct text, but the 3rd row has the 1st row's text overlaid with the 3rd row's text.

Anyone explain what my problem is? Thanks

2
  • in case 2, why [cell.contentView addSubview:balance];?? besides that, see Kenny's answer.
    – mvds
    Mar 30, 2011 at 1:02
  • Actually, that's what caused the problem. I looked at this code for an hour and never saw it. Thank you, and will change re Kenny's answer.
    – DNewell
    Mar 30, 2011 at 1:09

1 Answer 1

0

Yes, you are adding subviews if the dequeueReusableCellWithIdentifier returns a cell. In this case you are adding subviews to a cell that already has been created and subviews added. Suggest only adding the subviews when dequeueReusableCellWithIdentifier returns nil.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.