我试图在UITableView单元格内添加另一个 View Controller 。这个想法是您点击单元格,它会展开以显示更多内容-消息传递界面。重要的是(我认为)这由单独的Messaging ViewController控制。
在Storyboard中,扩展单元格并在适当的约束下使单元格内部的 View 扩展非常简单,因此我尝试通过容器将新的VC添加到TableViewCell中,从而将所有内容保留在Storyboard中。这样,我就可以在容器 View 上添加约束,并通过Messaging VC传递内容。
这是错误:
有什么方法可以解决此问题,或者有什么方法可以将 View Controller 中的 View 通过管道传递到此tableviewcell中,并将其约束到我在 Storyboard 中设置的配置?谢谢!
最佳答案
我有相同的任务,并以此方式决定:
步骤1. 创建子类MyCell: UITableViewCell
。
步骤2。如果使用Self-Sizing Cells
,则在InterfaceBuilder中将UIView添加到MyCell,然后将高度约束和约束添加到所有面。此 View 用于设置像元高度。
如果不是,请跳过此步骤并使用heightForRowAtIndexPath
。
步骤3。在MyCell.h中,添加 View 高度约束和 Controller 属性的导出:
@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.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;
}
步骤5. 添加
didEndDisplayingCell
方法:- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[MessagingVC class]])
[((MyCell*)cell).controller removeFromParentViewController];
}
关于ios - UITableViewCell中的iOS容器 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21345629/