iOS Container View in UITableViewCell
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
.
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];
}
Make your UITableViewController
content as Static
.