Two Tableviews in One view controller and One tableview not working when setting DS for 2nd one

StackOverflow https://stackoverflow.com/questions/22162104

  •  19-10-2022
  •  | 
  •  

Question

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.

No correct solution

OTHER TIPS

if you are using 2 tableview or 1 tableview try to set tag values for it with two different values suppose for table1 tag is 50 and for table2 tag is 60 then the code in tableview delegates and datasource should be like below.

-(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;
}

You can use any custom control that suits your requirement from cocoacontrols one of them is

MVYSideMenu
You don't require to add two tableview in one ViewController

You should try below approach:

STEPS:

  1. Add two Prototype cells to storybaord

  2. Make two custom cell class subclassing UITableViewCell and design your cell storyboard or via code

  3. cellForRowAtIndexPath: initialize cell according to requirement and set it datasource and delegate methods accordingly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top