Autolayout in UIView where is safe to ask computed layout values?

不想你离开。 提交于 2020-01-05 07:48:51

问题


I've got this issue. I'm using a collection view as a subview of the main view of a UIViewController. The collection view is pinned to the superview size, thus it has 4 constraints for lead,trail, top and bottom with constants equal to 0. The cell is a subclass to UICollectionViewCell and is composed by:

  1. first header view
  2. second header view
  3. UITableView

The collection view cell is loaded from xib and the interface of the views is made for 4 inches devices.
The first header view is pinned at top, trail, lead with constant 0 and fixed height to its superview.
The second header view is constrained to first header view with vertical spacing equal to 0, fixed height, pinned trail, lead with constant 0 to its superview.
The table view is constrained to second header view with vertical spacing equal to 0 and pinned trail, lead and bottom with constant 0 to its superview.
On 4 inches screen everything is OK, but when I load on 3.5 I have some problems, the fact is that I want to create UITableViewCell with a dynamic height. The height should be the height of the UITableView dived by the number of rows, in this way they will appear on screen.
The table view delegate and datasource are valorized only when I set the data that need to be loaded and that happens after creating it, but before the collection view cell is returned from the data source method.
In the -layoutSubviews method of the UICollectionViewCell subclass I set the tableview row height, to the desired value that is

- (void) layoutSubviews {
    [super layoutSubviews];
    self.answerTableView.rowHeight = self.answerTableView.bounds.size.height / self.quizSection.answers.count;
}

The fact is that here the table view is stil not resized thus the resulting rowHeight is wrong, the value is the same I receive in 4 inches display. The superview (the contentView of the collection view cell) is ok, but the table view has a size that is not correct. When displayed the collection view is fine except for the fact that the table view's cells are wrong in size. So I started to trace the cycle of the collection view cell.

  1. In the initWithFrame methods the collection view cell has the same size of the original xib (4 inches screen)
  2. In the view controller if I ask the size of the collection view cell right after dequeuing, if resized to the correct screen size, but the table view inside not
  3. After loading the data and setting delegate and datasource, collection view is ok, but tableview not
  4. In the -updateConstraints of the collection view cell the table view size is still wrong
  5. In the -layoutSubviewsof the collection view cell the table view size is still wrong
  6. The first call of the dtasource method of the table view returns a correct size

I've found the solution that is use the delegate methods - (float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath, but I'm really curios why the other approach doesn't work can someone explain why?
THX
[CODE FOR HELP]

- (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;
}

来源:https://stackoverflow.com/questions/17122284/autolayout-in-uiview-where-is-safe-to-ask-computed-layout-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!