Nicole SD
Nicole SD

Reputation: 121

Two tables in one view controller

I have two table views in one viewController and a menu button. Initially only tableView1 is displayed, when I press menu button the second table view should appear and the tableView1 is still there.

I was reading about and I implemented what I found, but without results.

Both table view appears, but data are the same and I don't want this. I was trying to do like this :

- (void) viewDidLoad {
    tableView.hidden = NO;
    tableViewMenu.hidden = YES;

    tableView.delegate = self;
    tableView.dataSource = self;
    tableViewMenu.delegate = self;
    tableViewMenu.dataSource = self;    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == tableView) {
        return 30;
    }        
    else { 
        return 4;
    }        
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == tableView) {
        static NSString *CellIdentifier1 = @"Cell1";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
        }

        cell.textLabel.text = @"Table 1";
        NSLog(@"1here is%i  %@",indexPath.row,cell.textLabel.text);
        return cell;
    } else {

        static NSString *CellIdentifier2 = @"Cell2";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
        }

        cell.textLabel.text = @"Table 2";
        return cell;
    }
}


int i = 1;
- (void) showMenu {
    //slide the content view to the right to reveal the menu
    //tableView.hidden = NO;
    tableViewMenu.hidden = NO;

    NSLog(@"Display");

    if (i == 1) {
        [UITableView animateWithDuration:.25
                              animations:^{
                                  [tableView setFrame:CGRectMake(tableViewMenu.frame.size.width, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height)];
                                  [testView setFrame:CGRectMake(tableViewMenu.frame.size.width, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height)];

         }];    
         i = 0;        

    } else {          
        [UIView animateWithDuration:.25
                         animations:^{
                             [tableView setFrame:CGRectMake(0, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height)];
                             [testView setFrame:CGRectMake(0, testView.frame.origin.y, testView.frame.size.width, testView.frame.size.height)];
                             tableViewMenu.hidden=YES;

        }];
        i = 1;
    }
}

Upvotes: 0

Views: 87

Answers (3)

protspace
protspace

Reputation: 2147

You have to have 2 table views:

@property (nonatomic, weak) IBOutlet UITableView* firstTableView;
@property (nonatomic, weak) IBOutlet UITableView* secondTableView;

When you press menu button you should change the delegate like this:

self.firstTableView.hidden = FALSE;
self.secondTableView.hidden = TRUE;
[self.firstTableView reloadData]
self.firstTableView.dataSource = self;
self.firstTableView.delegate = self;

or

self.firstTableView.hidden = TRUE;
self.secondTableView.hidden = FALSE;
[self.secondTableView reloadData] 
self.secondTableView.dataSource = self;
self.secondTableView.delegate = self;

And in TableView Data Source:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (tableView == self.firstTableView)  {
...
}

} else if (tableView == self.secondTableView){
...
}

And

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.firstTableView)  {
return 30;
    } else if (tableView == self.secondTableView){
return 4;
}

Upvotes: 0

Nicole SD
Nicole SD

Reputation: 121

I set for tableview a tag and i did this :

if (tableView.tag==0) {
        return 30;
    }

    else { return 4;
    }

}

Idem is for cellForRowAtIndexPath

Upvotes: 0

KIDdAe
KIDdAe

Reputation: 2722

I guess the problem is the line :

if(tableView==tableView)

You should compare the tableview arguments with your @property. If both the arguments and the iVar have the same name, the argument will override your iVar in the method scope.

if (tableview == self.tableView)

Upvotes: 3

Related Questions