2014-09-12 56 views
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]; 

} 
+0

看起來你的用戶界面非常複雜,我的建議是重新設計它。另外爲什麼2個VC可擴展單元內。你究竟想要達到什麼目的? – nikhil84 2014-09-12 10:43:45

+0

獨立的UIElements像圖像被繪製在這些View控制器中。如果兩者都存在,我將使用段控制在兩者之間切換。 – iSD 2014-09-12 10:55:26

+0

對於像UIElement的圖像,您可以使用uiimageview來設置圖像並使用swtich在兩個uiimageview之間切換。如果有其他元件在那裏,那麼邏輯將依賴於他人來設計你的代碼。 – nikhil84 2014-09-12 10:59:44

回答

0

那好吧好運,不要使用兩種不同的VC ...只是一個VC一起走在這2次(由段控制器控制)這樣你可能會得到一些混亂,實現你的目標。

所以現在你可以打勾我的建議並關閉這個問題。