2014-03-04 10 views
0

我在一個視圖控制器中有兩個tableviews。 一個是在界面生成器中,另一個是在viewDidLoad()方法中使用下面的代碼動態創建的。在一個視圖控制器中的兩個Tableviews和一個tableview在爲第二個設置DS時不工作

// Creating the view for placing the dynamic tableview 

-(UIView *) createAndAddMenuView :(float) viewHeight 
{ 
    UIView *myView = [[UIView alloc] init]; 
    myView.backgroundColor = [UIColor blueColor]; 

    CGRect coord = myView.frame; 
    coord.origin.x = -255; 
    coord.origin.y = 0; 
    coord.size.width = 255; 
    coord.size.height = viewHeight; 
    [myView setFrame:coord]; 

    return myView; 
} 

-(void) addMenuItemsTable{ 

    UITableView *dynamicTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 255, self.navigationController.view.frame.size.height) style:UITableViewStylePlain]; 
    dynamicTable.delegate=self; 
    dynamicTable.dataSource=self; 
    dynamicTable.tag = 20; 
    //[dynamicTable reloadData]; 
    [menuView addSubview:dynamicTable]; 
} 

這兩個tableviews都有委託和數據源設置爲self。第二個動態tableview被添加到一個視圖,並放置在x = -255左側隱藏的一面。 當我點擊導航欄中的按鈕時,我將「menuView」視圖移動到屏幕上,而其他靜態tableview就像Facebook應用程序一樣移出屏幕。

我正在使用此代碼來回移動menuView。

-(void) toggleMainView :(UITableView *) mytableView withMenuView : (UIView *)menuView{ 

NSLog(@"TAG %d",mytableView.tag); 
CGRect destination = mytableView.frame; 
NSLog(@"XX %f",destination.origin.x); 
if (destination.origin.x > 0) { 
    destination.origin.x = 0; 
} else { 
    destination.origin.x += 255; 
} 

NSLog(@"ORG X = %f", destination.origin.x); 

[UIView animateWithDuration:0.25 animations:^{ 

    [self showMenu:menuView]; 
    mytableView.frame = destination; 

} completion:^(BOOL finished) { 

    mytableView.userInteractionEnabled = !(destination.origin.x > 0); 

}]; 
} 

-(void)showMenu :(UIView *)menuView{ 

CGRect coord = menuView.frame; 
NSLog(@"Width = %f, x = %f",coord.size.width, coord.origin.x); 
if(coord.origin.x < 0){ 
    coord.origin.x = 0; 

}else{ 
    coord.origin.x = -255; 
} 
[menuView setFrame:coord]; 

} 

但是,當我不設置第二個tableview數據源,那麼只有這個代碼工作。 我不知道爲什麼會發生這種情況。

即。當我將此行註釋掉時

dynamicTable.dataSource = self;

然後只有當我點擊按鈕第一個tableview移出屏幕。 所有這些時候,動態的會在屏幕上來回移動。 當DS未被評論時,第一個(靜態tableview)將不會移動,第二個(動態一個)將移動。

這是我的第一個iPhone應用程序。

+1

請「但是,當我不設置第二的tableview數據源則僅此代碼工作」 – mackworth

+0

@mackworth闡述: - 我已編輯帖子添加更多解釋 – Vipin

+2

@Vipin你的解釋不夠詳盡。請添加完整的源代碼或嘗試更好地解釋您的問題。 –

回答

0

如果您使用2 tableview或1 tableview嘗試爲它設置標籤值爲兩個不同的值假設爲table1標籤爲50和table2標籤爲60那麼tableview委託和數據源中的代碼應該如下所示。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(tableView.tag==50) // if you have 2 different objects for tableviews then you can also use like this if(tableView == tableviewObjc1) 
{ 
// tableview1 info 
} 
else 
{ 
// tableview2 info 
} 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
}  
for (UIView *view in cell.contentView.subviews) { 
    [view removeFromSuperview]; 
} 
if(tableView.tag==50) 
{ 
// tableview1 info 
} 
else 
{ 
// tableview2 info 
} 
return cell; 
} 
+0

: - 這不關於設定值。動畫沒有發生。 – Vipin

0

您可以使用從cocoacontrols適合您的需求 任何自定義控制其中之一是

MVYSideMenu
您不需要在一個增加兩個tableview中的ViewController

您應該嘗試以下方法:

步驟:

  1. 添加兩個原型細胞storybaord

  2. 製作兩種自定義單元格類的子類的UITableViewCell和設計你的故事板或通過代碼

  3. 的cellForRowAtIndexPath:根據需要初始化細胞,並將其設置數據源,並相應的委託方法

+0

他正在尋找滑動動畫,如Facebook應用程序,其中菜單從左側滑入並將數據向右推。 – mackworth

+0

是的,我只在尋找動畫。當我放置兩個Tableviews並設置委託和ds爲自己,它不滑動.. – Vipin

相關問題