UITableViewCell中的iOS容器视图 [英] iOS Container View in UITableViewCell
问题描述
我正在尝试在UITableView单元格中添加另一个视图控制器。我们的想法是你点击单元格,它会扩展以显示更多内容 - 一个消息传递界面。重要的是(我认为)这是由一个单独的Messaging ViewController控制的。
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.
扩展单元格并使用适当的约束扩展单元格中的视图实际上非常简单故事板,所以我试图通过Container将我的新VC添加到TableViewCell来保留故事板中的所有内容。这样我就可以在容器视图中添加约束,并从我的Messaging VC中管理内容。
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.
这是错误:
非法配置:容器视图不能放在运行时重复的元素中。
Illegal Configuration: Container Views cannot be placed in elements that are repeated at runtime.
有什么方法可以解决这个问题,还是有办法将视图从我的viewcontroller传递到这个tableviewcell并限制我在Storyboards中设置的配置?谢谢!
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!
推荐答案
我有同样的任务,并以这种方式决定:
I had the same task and decided it this way:
第1步。创建子类 MyCell:UITableViewCell
。
步骤2. 如果使用 Self-Sizing Cells
,在InterfaceBuilder中将UIView添加到MyCell,然后向所有边添加高度约束和约束。此视图需要设置单元格高度。
如果没有,请跳过此步骤并使用 heightForRowAtIndexPath
。
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
.
第3步:在MyCell.h中,从视图高度约束和控制器属性添加插座:
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
第4步。在 cellForRowAtIndexPath
添加代码:
- (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.controller didMoveToParentViewController:self];
[cell.contentView addSubview:cell.controller.view];
// if you use Self-Sizing Cells
cell.viewHeight.constant = 200; // set your constant or calculate it
return cell;
}
第5步。添加 didEndDisplayingCell
方法:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[MessagingVC class]])
[((MyCell*)cell).controller removeFromParentViewController];
}
这篇关于UITableViewCell中的iOS容器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!