简体   繁体   中英

Xcode: add TableView into custom Cell

I'm trying to do something but neither found some example on the web or know if it is possible.

I need to add a table into the first cell of a tbaleview programmatically.

The code gives me problem when I try to set delegate and datasource of the second table (the one in the first table)

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

    [[NSBundle mainBundle] loadNibNamed:@"CellaMenu" owner:self options:NULL];
    cell = cellaMenu;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.userInteractionEnabled = NO;
    tabellaMenu = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tabellaMenu.dataSource = self;
    tabellaMenu.delegate = self;
    [cell.contentView addSubview:tabellaMenu];
}

In this way the code loops. If I'don't set the delegate and the dataSource the table appears but I need them to create custom handler. Any hints?

最干净的方法是在视图控制器中具有另一个符合UITableViewDataSource和UITableViewDelegate的对象,然后将第二个表视图的委托和dataSource设置为此对象。

The code is going into loop because tabellaMenu table in the cell has the delegate and datasource set to self. So, it keeps on calling table datasource methods on self and create new cells and enters into loop.

You can either create a separate object(subclass of NSObject) and define the table delegate and datasource methods in that and set it to tabellaMenu's delegate and datasource.

Or you can create a subclass of UITableViewCell and create a table view in that programatically. Define the table's datasource and delegate methods in that. So every table view in the cell will refer to its own cell for datasource and delegate. In addition, you get -(void)prepareForReuse(if the cell is reusable) to reload the table in the cell everytime the main table reloads.

Inside -tableView:cellForRowAtIndexPath: , you could write following code after creating cell.

 //if first row & table is not present already
if (indexPath.row == 0 && ![cell.contentView viewWithTag:5]) {

    UITableView *tabellaMenu =
        [[UITableView alloc] initWithFrame:self.contentView.bounds
                                     style:UITableViewStylePlain];    
    tabellaMenu.tag = 5;
    tabellaMenu.dataSource =tabellaMenuDataSource;
    tabellaMenu.delegate = tabellaMenuDelegate;

    [cell.contentView addSubview:tabellaMenu];
  }

You can get the project from here which i created while learning CoreData.Just click on Add button in navigation bar and Add information and save that. Then you will the data in the Table on Main Screen. Just click on the cell and there is another tableView.

Here is the way:

If you need a tableView only in first cell then you can take it globally instead of initialising in cellForRowAtIndexPath: method as it gets recall every time during scrolling.

UITableView *mainTableView;
UITableView *childTableView;

Now you can easily distinguish both tables in their delegates and datasources methods by comparing their instances.

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

    if (tableView == mainTableView) {
       // Do code for your main tableView

       if (indexPath.row == 0) {

           [[NSBundle mainBundle] loadNibNamed:@"CellaMenu" owner:self options:NULL];
           cell = cellaMenu;
           cell.selectionStyle = UITableViewCellSelectionStyleNone;
           cell.userInteractionEnabled = NO;

           if (!childTableView) {
               // Initialise only if it is nil (not initialised yet)
               childTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
               childTableView.dataSource = self;
               childTableView.delegate = self;
           }

           [cell.contentView addSubview:childTableView];
       }

    }
    else {
         // Do code for your child tableView
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM