0
我有一个tableView与展开的单元格。在这个展开的单元格中,我有一个UIView,在其上我添加了两个不同的viewControllers
。现在如果我同时添加了viewControllers
我添加了一个UISegmentControl
在它们之间切换。不过问题是这些viewControllers
似乎在某些细胞中添加到subView
,从而显示随机行为。如何在UITablViewCell中正确添加ChildViewControllers?
让我们来看看我是如何实现这一目标的。
(我做在XIB UI)
1]在细胞扩增 - >呼叫web服务 - >使用响应于上绘制viewControllers
和负载的一个或两个的数据viewControllers
。 2]当展开的单元格被删除 - >删除viewControllers
。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.expandedCells containsObject:indexPath]) {
[self.expandedCells removeObject:indexPath];
if (lowerVC) { //ViewController1
[self removeContentController:lowerVC];
}
if (upperVC){ //viewController2
[self removeContentController:upperVC];
}
}else{
isExpanded=YES;
[self.expandedCells addObject:indexPath];
//call webservice here with completion handler, when completion done, populate viewContollers and add as subView.
}
[self.bTableView beginUpdates];
[self.bTableView reloadData];
[self.bTableView endUpdates];
}
-(void)populateViewControllers{
//After populating VC's
[self displayContentController:lowerVC];
}
添加和删除viewControllers
的方法是像
-(void)displayContentController:(UIViewController *)content{
[self addChildViewController:content];
content.view.frame = CGRectMake(0,0, expCell.expContainer.frame.size.width, expCell.expContainer.frame.size.height);
[expCell.expContainer addSubview:content.view];
[content didMoveToParentViewController:self];
}
-(void)removeContentController:(UIViewController *)VC{
[VC willMoveToParentViewController:nil];
[VC.view removeFromSuperview];
[VC removeFromParentViewController];
}
看起来你的用户界面非常复杂,我的建议是重新设计它。另外为什么2个VC可扩展单元内。你究竟想要达到什么目的? – nikhil84 2014-09-12 10:43:45
独立的UIElements像图像被绘制在这些View控制器中。如果两者都存在,我将使用段控制在两者之间切换。 – iSD 2014-09-12 10:55:26
对于像UIElement的图像,您可以使用uiimageview来设置图像并使用swtich在两个uiimageview之间切换。如果有其他元件在那里,那么逻辑将依赖于他人来设计你的代码。 – nikhil84 2014-09-12 10:59:44