Ofir Malachi
Ofir Malachi

Reputation: 1286

Set UITableView Cell height (contain dynamic UICollectionView)

MY GOAL: Each UITableView Cell Contain 1 UIColletionViewController (dynamic items count)

PROBLEM: UITableView 'HeightForRow's delegate, is call before UIColletionViewController finish load his view + height.

RESULTS: UITableView 'HeightForRow = 0

How can i set the UITableView "HeightForRow" delegate correctly? the UIColletionViewController's content height haven't loaded yet

enter image description here

the result should be like in the following draw:

enter image description here

ALREAD TRY:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 300.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

   //not working well
    return UITableViewAutomaticDimension;

    //also tried this:
    ResultsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    Object * object = [_array objectAtIndex:indexPath.section];
    [cell setCellObject:object]; //this is build the cell ui
    return cell.collection.rect.size.height;
}

Upvotes: 0

Views: 218

Answers (1)

Ofir Malachi
Ofir Malachi

Reputation: 1286

return the "content size" (instead of view.size)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    //1. get the cell (by identifier)
    ResultsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ResultsTableViewCell"];

    //2. build cell ui
    Object * object = [_array objectAtIndex:indexPath.section];
    [cell buildCell:object];

    //3. return content size
    return cell.collection.collectionView.contentSize.height;
}

Upvotes: 1

Related Questions