0

UITableview グループ化スタイルの 2 つのセクションに 1 つの問題があります。最初のセクションは 6 行、2 番目のセクションは 3 行です。テーブルビューを上下にスクロールすると、セクション2の最後の行のコンテンツが最初のセクションの最初の行に追加され、最初のセクションの最初の行のコンテンツが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 つの行があります。私が間違っているところ。この問題を解決するのを手伝ってください。

前もって感謝します。

4

2 に答える 2

1

コードを次のように変更して、再試行してください。

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

その理由は、セルを再利用していない場合にのみセルの内容を設定しているためです。ほとんどの場合、それは間違っています。

于 2011-12-01T12:16:41.350 に答える
0

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];
于 2011-12-01T12:27:13.130 に答える