0

私は最近、UITableView を使用しています。一度動的に入力された後、ユーザーがオプションを選択すると、リストを新しいものに変更したいと考えています。3 つのセクションを持つグループ化されたテーブルを使用しています。行をクリックすると、3 つのグループにさまざまな数の新しい行を再入力する必要があります。新しいセクションに古いセクションと同じ数の行がある場合、私のコードは正常に動作しますが、その数が変わるとクラッシュします。ただし、興味深いことに、以前にあったセルの 1 つを描画しようとするまでクラッシュするのを待ちます (tableView は、セクションに古い行数があるとまだ考えており、モデル オブジェクトに存在しなくなったセルを描画しようとし、存在しない新しい配列の値を参照しているため、クラッシュすると思います。

ここでクラッシュします:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath{

UITableViewCell *cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

if (cell == nil){

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:@"MyCell"];
}

if (indexPath.section==2){
//CRASH BELOW
    cell.textLabel.text = [NSString stringWithFormat:@"%@", (NSString *)[[[[storyArray objectAtIndex:pageNumber]getChoices] objectAtIndex:(unsigned long)indexPath.row]objectAtIndex:0]] ;
}
return cell;
}

テーブルをリロードするために使用する関数は次のとおりです。

-(void)changePage:(int)pageChangeNumber{

NSLog(@"The page change! Changing to: %@",[[storyArray objectAtIndex:pageChangeNumber]getTitle]);
pageHeader.text=[[storyArray objectAtIndex:pageChangeNumber] getTitle];
pageBody.text=[[storyArray objectAtIndex:pageChangeNumber] getBody];

[myTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

[myTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];

 [myTableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade];

   [myTableView reloadData];    
pageNumber=pageChangeNumber;
NSLog(@"Page Change Done");
}

numberofRowsInSection も動的に変更しました...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"Recounting Rows");
if (section==2){
    return [[[storyArray objectAtIndex:pageNumber]getChoices] count];
} else {
    return 0;
}

}

セクションごとの行数が変化したときにこれを機能させる方法についてのアイデアはありますか? ありがとう!

4

2 に答える 2

1

発生しているクラッシュはわかりませんが、テーブルが再構築されているときにnumberOfSectionsInTableView:orメソッドが異なる行数を返すと、クラッシュが発生しました。tableView:numberOfRowsInSection:

たとえば、UITableViewは、再描画中(アニメーション中を含む)にこれらのメソッドを何度も呼び出します。私のバックエンドでは、アニメーション中にいくつかの値が変化していました。

UITableViewを変更する前に、これらの値を同期するために特別な注意を払う必要がありました

于 2012-09-07T22:22:21.270 に答える
1

テーブル ビューのデータを更新して reloadSections を呼び出す前に、最初に呼び出す必要があり[myTableView beginUpdates]ます。[myTableView endUpdates]

于 2013-02-05T14:12:04.297 に答える