2014-09-04 72 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

相關問題