私は最近、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;
}
}
セクションごとの行数が変化したときにこれを機能させる方法についてのアイデアはありますか? ありがとう!