これには少し説明が必要かもしれません。すぐ下にラベルとテーブルビューを含む UIViewController サブクラスがあります (ラベルとその他のコントロールが UITableViewController サブクラスを使用できない理由です)。テーブルビューの内容は動的であるため、追加時に高さを直接設定することはできません。そのため、loadView で次のことを行っています。
//add the table view with no size for now
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
tableView.scrollEnabled = NO;
//now that the datasource is set, use the delegates to update the height
[tableView layoutIfNeeded];
[tableView setFrame:CGRectMake(0, self.y_position, 320, [tableView contentSize].height)];
//add it to my scrollview, which contains all controls in this view
[scrollView addSubview:tableView];
これは今のところうまく機能しています (ただし、別のアイデアを聞くとよいでしょう)。
ビューの編集モードに入ると問題が発生します。追加のセクションと行を挿入して、テーブル ビューの内容を変更したいと考えています。これまでのところ、setEditing の次の実装があります。
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
[tableView beginUpdates];
[super setEditing:editing animated:animated];
//insert or remove the section and row
NSRange range = NSMakeRange(0, 1);
if (self.isEditing) {
[tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:range] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else {
[tableView deleteSections:[NSIndexSet indexSetWithIndexesInRange:range] withRowAnimation:UITableViewRowAnimationAutomatic];
}
[nameAndIngredientsTableView endUpdates];
}
追加のセクションと行は問題なく追加されていますが、サイズが更新されていないため、テーブル ビューの一番下の行が切り取られています。どうすればいいですか?
同じコードをもう一度使用しようとしました-layoutIfNeededを使用してから、高さを手動で設定しましたが、これは何もしないようです
編集に出入りするときにテーブルビューのサイズを動的に変更するために何を見なければならないかを知りたいです。