私は最近、UITableViewで作業しています。動的に1回入力され、ユーザーがオプションを選択すると、リストを新しいものに変更します。 3つのセクションを持つグループ化されたテーブルを使って作業しています.3つのグループを変更するには、3つのグループを変更する必要があります。 新しいセクションに古いものと同じ数の行がある場合、コードが正常に機能しますが、その番号が変更されるとクラッシュします。興味深いことに、以前はそこにあったセルの1つを描画しようとするまでクラッシュするのを待っています(tableViewはまだセクションに古い数の行があると思っていますが、モデルオブジェクトにないセルを描画しようとします。そして私はそれが存在しない新しい配列の値を参照していますので、それがクラッシュしたと思います異なる数の行を持つ1つのセクションにUITableviewをリフレッシュする
ここはクラッシュ:。
- (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;
}
}
セクションごとの行数が変更されたときにどのようにしてこの動作が得られるかについてのアイデアはありますか? ありがとう!