Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Container View in UITableViewCell

I'm trying to add another view controller inside a UITableView cell. The idea is that you tap the cell, and it expands to show more content--a messaging interface. It's important (I think) that this is controlled by a separate Messaging ViewController.

Expanding the cell and having views inside the cell expand with the proper constraints is actually very straightforward in Storyboards, so I tried to keep everything in storyboards by adding my new VC to the TableViewCell via a Container. That way I'd be able to add constraints on the container view, and pipe the content in from my Messaging VC.

Here's the error:

Illegal Configuration: Container Views cannot be placed in elements that are repeated at runtime.

Any way to get around this issue, or is there a way I can pipe the view from my viewcontroller into this tableviewcell and have it constrain to a configuration that I set in Storyboards? Thank you!

like image 992
Rob Avatar asked Jan 25 '14 01:01

Rob


2 Answers

I had the same task and decided it this way:

Step 1. Create subclass MyCell: UITableViewCell.

Step 2. If you use Self-Sizing Cells, in InterfaceBuilder add UIView to MyCell, then add height constraint and constraints to all sides. This view needed for set height of cell.
If not, skip this step and use heightForRowAtIndexPath.

enter image description here enter image description here

Step 3. In MyCell.h add outlet from view height constraint and controller property:

@interface MyCell: UITableViewCell  @property (weak, nonatomic) MessagingVC *controller; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;  @end 

Step 4. In cellForRowAtIndexPath add code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {      MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];      // adjust this for your structure      cell.controller = [[UIStoryboard storyboardWithName:@"MessagingVC" bundle:nil] instantiateInitialViewController];      [self addChildViewController:cell.controller];     [cell.contentView addSubview:cell.controller.view];     [cell.controller didMoveToParentViewController:self];      //  if you use Self-Sizing Cells     cell.viewHeight.constant = 200; // set your constant or calculate it      return cell; } 

Step 5. Add didEndDisplayingCell method:

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {     if ([cell isKindOfClass:[MessagingVC class]])          [((MyCell*)cell).controller removeFromParentViewController]; } 
like image 92
Igor Avatar answered Sep 24 '22 10:09

Igor


Make your UITableViewController content as Static.

enter image description here

like image 38
thatzprem Avatar answered Sep 23 '22 10:09

thatzprem