0

UITableView でセクションを展開しようとすると、単一のセクションが展開されて閉じられた場合は問題ありませんが、セクションが展開された場合、前のセクションを閉じずに別のセクションがクラッシュします。以下は私が試している私のコードです。

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(!helpOn)
    return 1;
else
    if(section == selectedCellIndexPath)
    {
    return 2;
    }
    else{
        return 1;
    }
return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell;
cell = [self.mHelpTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
else{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

UILabel *txtQues = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 310, 30)];
txtQues.backgroundColor = [UIColor clearColor];
txtQues.lineBreakMode = NSLineBreakByWordWrapping;
txtQues.numberOfLines = 2;
txtQues.userInteractionEnabled = FALSE;

UITextView *txtAns = [[UITextView alloc]initWithFrame:CGRectMake(5, 10, 310, 60)];
txtAns.backgroundColor = [UIColor clearColor];
txtAns.userInteractionEnabled = FALSE;

txtQues.font = [UIFont fontWithName:@"Helvetica-Bold" size:13.0];

if(!helpOn)
//if (indexPath.section==selectedCellIndexPath)
{
    if(indexPath.row == 0)
        [cell.contentView addSubview:txtQues];
        txtQues.text = [self.mArrQues objectAtIndex:indexPath.section];
}
else
{
    if(indexPath.row == 0)
    {
        [cell.contentView addSubview:txtQues];
        txtQues.text = [self.mArrQues objectAtIndex:indexPath.section];
    }
    else{
        [cell.contentView addSubview:txtAns];
        txtAns.text = [self.mArrAns objectAtIndex:indexPath.section];
    }
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
helpOn = !helpOn;

int ind = indexPath.section;
if(ind == selectedCellIndexPath)
{
}
else{
    helpOn = YES;
}
if(helpOn)
{
    selectedCellIndexPath = indexPath.section;
[self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
    if(indexPath.row == 0)
    {
    //selectedCellIndexPath = indexPath.section;
    [self.mHelpTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
    }
}
}

上記のガイドをお願いします。ここですでに夕方と朝を過ごしました。セクション メソッドの行数でクラッシュします。以下はエラー取得です。

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
4

1 に答える 1

1

エラー メッセージを読んで、numberOfRowsInSectionおよびのロジックを確認してくださいdidSelectRowAtIndexPath。それは簡単に言います:

無効な更新: セクション 0 の行数が無効です。更新後の既存のセクションに含まれる行数 (2) は、更新前にそのセクションに含まれる行数 (1) にプラスまたはマイナスの数値を指定する必要があります。そのセクションから挿入または削除された行数 (0 挿入、0 削除)、およびそのセクションに移動された、またはそのセクションから移動された行数 (0 移動、0 移動)。

これを iOS ですでに説明されている以上に、これ以上明確に説明することは困難です。おそらくテーブルをリロードすると、テーブルは、セクション 0 (最初のセクション) の行数が更新間で異なり、許可されていないというエラーをスローします。テーブルを更新する前後に、セクション 0 にある行数を決定するロジックを確認してください。

[編集]

と設定するdidSelectRowAtIndexPath:とそのようになります。次に、そのセクションのテーブルをリロードすると、: が起動します。ifと the you return 2.これが、更新前に 1 が表示され、更新後に 2 が表示される理由である可能性があります。helpOn == YESselectedCellIndexPath = indexPath.sectionnumberOfRowsInSectionumberOfRowsInSection:helpOn == YESsection == selectedCellIndexPath

繰り返しますが、私のアドバイスは、これら 2 つの方法でロジックをチェックすることです。セクションを更新した後、セクションの 1 つの行を変更しています。

[編集2]

補足:cellForRowAtIndexPath毎回新しいセルを割り当てます。これは非効率的です。あなたif(cell == nil) { // create new cell }は他の必要はありません。

于 2013-08-01T19:00:30.907 に答える