2014-09-04 73 views
0

我想用两个UIViewssegmentControl。现在这两个UIViews都嵌入了own.These UIViews应该的scrollView显示为一台之间切换,所以我把它们放在对方(在XIB)的顶部。而当点击segmentControl时,我试图相应地隐藏/显示它们。但到目前为止,我无法在两者之间切换。下面还尝试了解决方案。但它只适用于随机单元,并且无法在所有单元中切换。缺少什么?如何通过使用段控制ios在两个UIViews之间正确切换?

这就是我如何设置隐藏在didSelectRow中的上部视图,其中我扩大了tableViewCell

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 


if ([self.expandedCells containsObject:indexPath]) { 

    expCell.upperContainer.hidden = NO; 
    expCell.upperScroll.hidden = NO; 
    [self.expandedCells removeObject:indexPath]; 

    }else{ 
    isExpanded=YES; 
    [self.expandedCells addObject:indexPath]; 

    //hide upper container 

    if (!expCell.upperContainer.hidden) { 

     expCell.upperContainer.hidden = YES; 


    } 
    if (!expCell.upperScroll.hidden) { 

     expCell.upperScroll.hidden =YES; 
    } 

    } 

    [self.bTableView beginUpdates]; 
    [self.bTableView reloadData]; 
    [self.bTableView endUpdates]; 
} 

然后在segmentControl的expandedCell上单击我正在执行以下操作。

-(void)selectDeckView:(UISegmentedControl*)sender{ 

if (sender.selectedSegmentIndex==0) { 
    NSLog(@"segment 0");    //executes 
    expCell.lowerDeckView.hidden=NO; 
    expCell.lowerScrollView.hidden=NO; 
    expCell.upperScroll.hidden=YES; 
    expCell.upperContainer.hidden=YES; 

}else if (sender.selectedSegmentIndex==1){ 
    NSLog(@"segment 1");    //executes 
    expCell.lowerDeckView.hidden=YES; 
    expCell.lowerScrollView.hidden=YES; 
    expCell.upperContainer.hidden=NO; 
    expCell.upperScroll.hidden=NO; 


} 

} 

回答

1

我建议让每个视图都在自己的视图控制器中,然后每当按下一个片段时添加适当的子视图控制器。

包含分段控件的视图控制器将是容器视图控制器,并且它会有一个像这样的方法添加两个内容视图控制器中的一个(每个控件都包含您提到的视图之一)当按下段调用:

- (void) displayContentController: (UIViewController*) content; 
{ 
    [self addChildViewController:content];     // 1 
    content.view.frame = [self frameForContentController]; // 2 
    [self.view addSubview:self.currentClientView]; 
    [content didMoveToParentViewController:self];   // 3 
} 

你可以缓存你的子视图控制器实例,让他们只得到创建一次,如果是适当的,这意味着它将会很快。苹果对内容/子视图控制器的说明文件:

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

+0

是的,我也想过这件事。不幸的是,我的视图和我的扩展表单元格一样,现在放在XIB中。如果这种情况甚至可能,我如何添加子视图。 – iSD 2014-09-04 13:15:07

+0

你可以将它们拆分成单独的nib,然后通过这样做来为每个nib创建一个子视图控制器:UIViewController * viewController = [[UIViewController alloc] initWithNibName:@“myNib”bundle:nil]; – pmacro 2014-09-04 13:28:00

+0

尝试解决您的问题。我有一个问题,因为我正在使用当前实现填充当前视图控制器中的视图。我可以从我的父视图控制器填充childViewControllers视图,然后加载它们?还是要做其他事情? – iSD 2014-09-05 11:47:56

0

你可能覆盖其他视图,你尝试过:

if (sender.selectedSegmentIndex==0) { 
    NSLog(@"segment 0");    //executes 
    expCell.lowerDeckView.hidden=NO; 
    expCell.lowerScrollView.hidden=NO; 
    expCell.upperScroll.hidden=YES; 
    expCell.upperContainer.hidden=YES; 
    [self.view bringSubviewToFront:expCell.upperScroll] 
    [self.view bringSubviewToFront:expCell.upperContainer] 

}else if (sender.selectedSegmentIndex==1){ 
    NSLog(@"segment 1");    //executes 
    expCell.lowerDeckView.hidden=YES; 
    expCell.lowerScrollView.hidden=YES; 
    expCell.upperContainer.hidden=NO; 
    expCell.upperScroll.hidden=NO; 
    [self.view bringSubviewToFront:expCell.lowerScrollView] 
    [self.view bringSubviewToFront:expCell.lowerDeckView] 


} 

} 

确保你把他们带到正面以正确的顺序。

+0

它实际上在随后的细胞在第一个单元格工作only..after不切换。 – iSD 2014-09-04 11:12:01

相关问题