Сгруппированное табличное представление в Iphone - PullRequest
0 голосов
/ 01 февраля 2011

Я использую сгруппированное табличное представление в моем приложении.У меня есть 2 раздела в таблице.Первый раздел имеет 3 строки, а второй раздел содержит более 10 строк. Когда я прокручиваю разделы, он показывает строки из раздела 1 после 5-го ряда второго раздела.что мне делать.

Вот мой код ...

- (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 голосов
/ 01 февраля 2011

Вам нужно 2 идентификатора ячейки.Проблема в том, что вы удаляете ячейки с одинаковым идентификатором.Вставьте код и должно работать нормально: D

PS: Я не решил ваши проблемы с вашими ярлыками.Вы распределяете их каждый раз, когда отображаете ячейку.Это неправильно.

#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 типа ячеек, вы используете 2 идентификатора, если у вас есть 1 тип, используйте один идентификатор и так далее.Идентификатор используется для удаления из очереди определенного типа ячеек, если ни одна ячейка не может быть повторно использована, она просто выделит новую.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...