私はこの問題を抱えています。のメイン ビューのサブビューとしてコレクション ビューを使用していUIViewController
ます。コレクション ビューはスーパービュー サイズに固定されているため、リード、トレイル、トップ、ボトムの定数が 0 の 4 つの制約があります。セルはサブクラスでUICollectionViewCell
あり、以下によって構成されます。
- 最初のヘッダー ビュー
- 2 番目のヘッダー ビュー
UITableView
コレクション ビュー セルは xib から読み込まれ、ビューのインターフェイスは 4 インチ デバイス用に作成されています。
最初のヘッダー ビューは、トップ、トレイル、リードに固定され、定数 0 で、高さがスーパービューに固定されます。
2 番目のヘッダー ビューは、最初のヘッダー ビューに制限され、垂直方向の間隔は 0 に等しく、高さは固定、トレイルはピンで固定され、そのスーパービューへのリードは定数 0 です。
テーブル ビューは、垂直方向の間隔が 0 に等しい 2 番目のヘッダー ビューに制限され、トレイル、リード、およびボトムが定数 0 でそのスーパービューに固定されます。4 インチの画面ではすべて問題ありませんが、3.5 にロードすると問題が発生します。実際には、動的な高さで
作成したいと考えています。UITableViewCell
高さはUITableView
このようにして、それらは画面に表示されます。
テーブル ビュー デリゲートとデータ ソースは、データを作成した後、データ ソース メソッドからコレクション ビュー セルが返される前に、ロードする必要があるデータを設定したときにのみ評価されます。サブクラス
の-layoutSubviews
メソッドで、テーブルビューの行の高さを目的の値に設定しましたUICollectionViewCell
- (void) layoutSubviews {
[super layoutSubviews];
self.answerTableView.rowHeight = self.answerTableView.bounds.size.height / self.quizSection.answers.count;
}
実際のところ、ここではテーブル ビューのサイズが変更されていないため、結果の rowHeight が間違っています。値は 4 インチ ディスプレイで受け取ったものと同じです。スーパービュー (contentView
コレクション ビュー セルの) は問題ありませんが、テーブル ビューのサイズが正しくありません。コレクション ビューを表示すると、テーブル ビューのセルのサイズが間違っていることを除けば問題ありません。そこで、コレクション ビュー セルのサイクルをトレースし始めました。
- initWithFrame メソッドでは、コレクション ビュー セルのサイズは元の xib (4 インチ スクリーン) と同じです。
- ビューコントローラーで、コレクションビューセルのサイズをキューから取り出した直後に尋ねた場合、正しい画面サイズにサイズ変更された場合、内部のテーブルビューはそうではありません
- データをロードし、デリゲートとデータソースを設定した後、コレクション ビューは問題ありませんが、テーブルビューは問題ありません
-updateConstraints
コレクション ビュー セルのテーブル ビューのサイズがまだ間違っている-layoutSubviews
コレクション ビュー セルのテーブル ビューのサイズがまだ間違っている- テーブル ビューの dtasource メソッドの最初の呼び出しで正しいサイズが返される
デリゲートメソッドを使用する解決策を見つけましたが- (float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
、他のアプローチが機能しない理由が本当に知りたいのですが、誰かが理由を説明できますか?
THX
[ヘルプコード]
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) { // Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
if ([arrayOfViews count] < 1) { return nil; }
if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; }
self = [arrayOfViews objectAtIndex:0];
[self.answerTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:AnswerCellIdentifier];
}
return self;
}
- (void) layoutSubviews {
[super layoutSubviews];
self.answerTableView.rowHeight = self.answerTableView.bounds.size.height / self.quizSection.answers.count; //WRONG
}
- (void) updateConstraints {
[super updateConstraints];
self.answerTableView.rowHeight = self.answerTableView.bounds.size.height / self.quizSection.answers.count; //WRONG
}
#pragma mark -
#pragma mark custom getter/setter
- (void) setQuizSection:(QuizSection *)quizSection {
if (_quizSection == quizSection) {
return;
}
_quizSection = quizSection;
//Set data
self.questionLabel.text = _quizSection.question[KEY_TEXT];
self.answerTableView.delegate = self;
self.answerTableView.dataSource = self;
}
#pragma mark -
#pragma mark TableViewDelegate TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.quizSection.answers.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:AnswerCellIdentifier];
NSDictionary * answerDict = self.quizSection.answers[indexPath.row];
cell.textLabel.text = answerDict[KEY_TEXT];
return cell;
}