Iphone中的分组表视图

时间:2011-02-01 09:35:42

标签: ios iphone uitableview

我在我的应用中使用分组表视图。我在表中有2个部分。第一部分有3行,第二部分有10行以上。当我滚动部分时,它会显示第5部分第5行后第1部分的行。我该怎么办。

这是我的代码......

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

    switch (section) {
        case 0:
            return 3;
            break;
            case 1:
            return 8;
                break;

        default:
            break;
    }

    return 5;
}


// Customize the appearance of table view cells.
- (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];
    }

    if (indexPath.section==0)
    {

        if (indexPath.row==0)
        {
            UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
            [Name setText:@"First Name "];
            [cell.contentView  addSubview:Name];

            UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
            textName.placeholder=@"Enter First Name";
            [textName setBorderStyle:UITextBorderStyleNone];
            textName.clearButtonMode = UITextFieldViewModeWhileEditing;
            textName.keyboardType=UIKeyboardTypeDefault;
            textName.returnKeyType=UIReturnKeyDone;
            textName.delegate=self;
            [cell.contentView addSubview:textName];


        }

    if (indexPath.row==1)
    {
        UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
        [Name setText:@"Middle Name"];
        [cell.contentView  addSubview:Name];

        UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
        textName.placeholder=@"Enter Middle Name";
        [textName setBorderStyle:UITextBorderStyleNone];
        textName.clearButtonMode = UITextFieldViewModeWhileEditing;
        textName.keyboardType=UIKeyboardTypeDefault;
        textName.returnKeyType=UIReturnKeyDone;
        textName.delegate=self;
        [cell.contentView addSubview:textName];


    }

    if (indexPath.row==2)
    {
        UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
        [Name setText:@"Last Name "];
        [cell.contentView  addSubview:Name];

        UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
        textName.placeholder=@"Enter Last Name";
        [textName setBorderStyle:UITextBorderStyleNone];
        textName.clearButtonMode = UITextFieldViewModeWhileEditing;
        textName.keyboardType=UIKeyboardTypeDefault;
        textName.returnKeyType=UIReturnKeyDone;
        textName.delegate=self;
        [cell.contentView addSubview:textName];


    }
    }


    if (indexPath.section==1) {

        if (indexPath.row==0) {
            cell.textLabel.text=@"1";
        }
        if (indexPath.row==1) {
            cell.textLabel.text=@"2";
        }
        if (indexPath.row==2) {
            cell.textLabel.text=@"3";
        }
        if (indexPath.row==3) {
            cell.textLabel.text=@"4";
        }
        if (indexPath.row==4) {
            cell.textLabel.text=@"5";
        }
        if (indexPath.row==5) {
            cell.textLabel.text=@"6";
        }
        if (indexPath.row==6) {
            cell.textLabel.text=@"7";
        }
        if (indexPath.row==7) {
            cell.textLabel.text=@"8";
        }


    }


    return cell;
}

1 个答案:

答案 0 :(得分:0)

您需要2个Cell标识符。问题是您使用相同的标识符对单元格进行出列。 粘贴代码,应该可以正常工作:D

P.S:我没有解决你的标签问题。每次显示单元格时都会分配它们。那是错的。

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

    NSInteger returnValue = 5;
    switch (section) {
        case 0:
        {
            returnValue = 3;
            break;
        }
        case 1:
        {
            returnValue = 8;
            break;
        }   
        default:
            break;
    }

    return returnValue;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifierSection1 = @"Cell1";
    static NSString *CellIdentifierSection2 = @"Cell2";

    UITableViewCell *cell;      
    if (indexPath.section==0)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection1];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection1] autorelease];
        }



        if (indexPath.row==0)
        {
            UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
            [Name setText:@"First Name "];
            [cell.contentView  addSubview:Name];

            UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
            textName.placeholder=@"Enter First Name";
            [textName setBorderStyle:UITextBorderStyleNone];
            textName.clearButtonMode = UITextFieldViewModeWhileEditing;
            textName.keyboardType=UIKeyboardTypeDefault;
            textName.returnKeyType=UIReturnKeyDone;
            textName.delegate=self;
            [cell.contentView addSubview:textName];


        }

        if (indexPath.row==1)
        {
            UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 110, 20)];
            [Name setText:@"Middle Name"];
            [cell.contentView  addSubview:Name];

            UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
            textName.placeholder=@"Enter Middle Name";
            [textName setBorderStyle:UITextBorderStyleNone];
            textName.clearButtonMode = UITextFieldViewModeWhileEditing;
            textName.keyboardType=UIKeyboardTypeDefault;
            textName.returnKeyType=UIReturnKeyDone;
            textName.delegate=self;
            [cell.contentView addSubview:textName];


        }

        if (indexPath.row==2)
        {
            UILabel *Name=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
            [Name setText:@"Last Name "];
            [cell.contentView  addSubview:Name];

            UITextField *textName=[[UITextField alloc]initWithFrame:CGRectMake(140, 10, 150, 20)];
            textName.placeholder=@"Enter Last Name";
            [textName setBorderStyle:UITextBorderStyleNone];
            textName.clearButtonMode = UITextFieldViewModeWhileEditing;
            textName.keyboardType=UIKeyboardTypeDefault;
            textName.returnKeyType=UIReturnKeyDone;
            textName.delegate=self;
            [cell.contentView addSubview:textName];


        }
    }


    if (indexPath.section==1) {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierSection2];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierSection2] autorelease];
        }

        if (indexPath.row==0) {
            cell.textLabel.text=@"1";
        }
        if (indexPath.row==1) {
            cell.textLabel.text=@"2";
        }
        if (indexPath.row==2) {
            cell.textLabel.text=@"3";
        }
        if (indexPath.row==3) {
            cell.textLabel.text=@"4";
        }
        if (indexPath.row==4) {
            cell.textLabel.text=@"5";
        }
        if (indexPath.row==5) {
            cell.textLabel.text=@"6";
        }
        if (indexPath.row==6) {
            cell.textLabel.text=@"7";
        }
        if (indexPath.row==7) {
            cell.textLabel.text=@"8";
        }


    }


    return cell;
}

编辑:

您可以为每种类型的单元格使用不同的标识符。因此,如果您有两种类型的单元格,如果您有一种类型使用一个标识符,则使用2个标识符,依此类推。标识符用于使特定类型的单元格出列,如果没有单元格可以重用,则只需分配一个新单元格。