I have two tableviews in one view controller.
One is there in the interface builder and the other i have created dynamically using the below code in the viewDidLoad() method.
// 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];
}
Both these two tableviews have delegate and datasource set to self. The second dynamic tableview is added to a view and placed on the left hidden side with x = -255.
When I click on the button in the navigation bar i am moving "menuView" view to the screen and the other static tableview out of the screen just like the Facebook app.
I am using this code for moving the menuView back and forth.
-(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];
}
But when I am NOT setting the second tableview Datasource then only this code is working.
I have no idea why this is happening.
ie. When I comment out this line
dynamicTable.dataSource=self;
Then only when I click the button the first tableview is moving out of the screen.
All these times the dynamic one will move back and forth in the screen.
When the DS is not commented the first (static tableview) will not move and Second (dynamic one) will move.
This is my first iPhone application.