hazelvan
hazelvan

Reputation: 133

Can I have another tableview inside a table view controller?

I have a TableViewController(tvc2) where instead of using a ViewController to show my details from another TableViewController(tvc1), I used tvc2. Basically, tvc2 is a grouped table that only shows detail. But then I need a another tableView(tv3) inside tvc2 that is a list. Each tableViewCell of the tableView should be segued to another detail ViewController.

My question is can I add the tableView inside the TableViewController? And if it's possible, how can I differentiate the numberOfSectionsInTableView, numberOfRowsInSection and cellRowAtIndexPath methods from the TableView to the TableViewController?

Upvotes: 0

Views: 1150

Answers (3)

amurcia
amurcia

Reputation: 791

Yes you have to do this. Example code:

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

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    if(row == 0){

            Grupo_DAO *grupo_bd = [[Grupo_DAO alloc] init];
            static NSString *CellTableIdentifier = @"PL";
            UINib *nib = [UINib nibWithNibName:@"PeriodoLectivoView" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];
            PeriodoLectivo *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
            computerGrupos = [grupo_bd ObtenerGrupos: identAsig];
            Grupo_DTO *grupoprueba = [[Grupo_DTO alloc] init];
            grupoprueba = [computerGrupos objectAtIndex:section];
            cell.Inicio = grupoprueba.inicio;
            cell.Fin = grupoprueba.fin;
            return cell;

    }else{
        ....
    }

I put in the row 0 of my UITableView another table. For the table iniside the UITableViewController you have to create a .m and .h files (in this example @"PeriodoLectivoView" -> ListaPeriodoLectivo.m). And in this files you have the methods umberOfSectionsInTableView, numberOfRowsInSection and cellRowAtIndexPath, etc.

Hope it helps.

Upvotes: 0

Valentin Shamardin
Valentin Shamardin

Reputation: 3658

YES, YOU CAN. When you create UITableView, you need to set dataSource and delegate. In your case dataSource and delegate for both tableViews is tvc2. In dataSource and delegate methods you need to fork code for each tableView (for example, tableView:numberOfRowsInSection:):

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

    if (tableView == tableView1)
    {
        //Your code
    }
    if (tableView == tableView2)
    {
        //Your code
    }
    if (tableView == tableView3)
    {
        //Your code
    }
}

Upvotes: 8

malex
malex

Reputation: 10096

It is very simple as in case of using UISearchDisplayController. You only need to check tableView in delegate/dataSource methods

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) return 1; // tvc1 is self
    if (tableView == tvc2.tableView) return 2;
    return 0;
}

Upvotes: 1

Related Questions