在iPhone中是否覆盖UITableview分区?

在iPhone中是否覆盖UITableview分区?,iphone,uitableview,sections,Iphone,Uitableview,Sections,我对UITableview分组样式中的两个部分有一个问题。第一部分有6行,第二部分有3行。当我上下滚动tableview时,第2节的最后一行内容添加到第一节的第一行,第一节的第一行内容添加到第二节的最后一行。我会检查我的级别,以确定单元格是否变空以加载内容,但调试时不会发生这种情况。我的密码是 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我对UITableview分组样式中的两个部分有一个问题。第一部分有6行,第二部分有3行。当我上下滚动tableview时,第2节的最后一行内容添加到第一节的第一行,第一节的第一行内容添加到第二节的最后一行。我会检查我的级别,以确定单元格是否变空以加载内容,但调试时不会发生这种情况。我的密码是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.backgroundColor = [UIColor whiteColor];

            if (indexPath.section == 0 && indexPath.row == 0) 
            {
                UILabel *Nameabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
                Nameabel.text = @"Name";
                Nameabel.font = [UIFont fontWithName:@"Helvetica" size:15];
                Nameabel.backgroundColor = [UIColor clearColor];
                Nameabel.font = [UIFont boldSystemFontOfSize:15];
                [cell.contentView addSubview:Nameabel];

                textField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
                textField.placeholder = @"Student Name";
                textField.borderStyle = UITextBorderStyleNone;
                textField.font = [UIFont fontWithName:@"Helvetica" size:14];
                [cell.contentView addSubview:paymentCatTextField];

            }
            else if (indexPath.section == 1 && indexPath.row == 0) 
            {
                UILabel *RLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
                RLabel.text = @"Class";
                RLabel.backgroundColor = [UIColor clearColor];
                RLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
                RLabel.font = [UIFont boldSystemFontOfSize:15];
                [cell.contentView RLabel];

                RTextField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
                RTextField.placeholder = @"class Name";
                RTextField.borderStyle = UITextBorderStyleNone;
                RTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                RTextField.font = [UIFont fontWithName:@"Helvetica" size:14];
                [cell.contentView addSubview:RTextField];
            }
        }
       return cell;
    }
这是样品。这样,节0有6行,节1有3行。我做错了什么。请帮我解决这个问题


提前感谢。

将代码更改为以下内容,然后重试:

{
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundColor = [UIColor whiteColor];
    }


    if (indexPath.section == 0 && indexPath.row == 0) {
         //bla bla
    } else if (indexPath.section == 1 && indexPath.row == 0) {
         //bla bla
    }

    return cell;
}

原因是,只有在不重用单元格时才设置单元格内容。这在大多数情况下都是错误的。

除了dasdom的答案之外,我还建议在添加标签之前删除子视图

if ([[cell.contentView subviews] count] > 0) {
    UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
    [labelToClear removeFromSuperview];
    UIView *textToClear = [[cell.contentView subviews] objectAtIndex:1];
    [textToClear removeFromSuperview];
}
和自动删除子视图:

UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease];
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease];

单元是可重用的,为了提高性能,您应该仔细阅读苹果的文档,UITableView的部分基本上是重要的。