UITableview разделы перезаписать в iPhone? - PullRequest
0 голосов
/ 01 декабря 2011

У меня одна проблема с двумя разделами в сгруппированном стиле UITableview. Первая секция имеет 6 рядов, а вторая секция имеет 3 ряда. Когда я прокручиваю вид таблицы вверх и вниз, содержимое последней строки раздела 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 строки. Где я делаю неправильно. Пожалуйста, помогите мне решить эту проблему.

Заранее спасибо.

Ответы [ 2 ]

1 голос
/ 01 декабря 2011

Измените свой код на следующий и повторите попытку:

{
 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;
}

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

0 голосов
/ 01 декабря 2011

В дополнение к ответу 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];
...