我在一個視圖控制器中有兩個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應用程序。
請「但是,當我不設置第二的tableview數據源則僅此代碼工作」 – mackworth
@mackworth闡述: - 我已編輯帖子添加更多解釋 – Vipin
@Vipin你的解釋不夠詳盡。請添加完整的源代碼或嘗試更好地解釋您的問題。 –