UITableView行定位错误

时间:2011-03-30 00:49:00

标签: iphone uitableview

我有一个tableview,其中有3行,每行有一个标签和一个文本字段。代码如下:

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

当我将其更改为2行时,将显示第1行和第2行,并且我可以在每个文本字段中输入值。当我将其更改为3行时,我无法选择第1行的文本字段。实际上,我添加了要加载到每个字段的文本,第二行具有正确的文本,但第3行的第1行的文本与第3行的文本重叠。

有人解释我的问题是什么吗?感谢

1 个答案:

答案 0 :(得分:0)

是的,如果dequeueReusableCellWithIdentifier返回一个单元格,则添加子视图。在这种情况下,您要将子视图添加到已创建的单元格并添加子视图。建议仅在dequeueReusableCellWithIdentifier返回nil时添加子视图。