-1

Lets say I have a tableview created in the storyboard and referenced as "tableViewJobList" and I want to generate a tableview in each cell of tableViewJobList so I do it in cellForRowAtIndexPath.

My .h file (tableViewJobList delegate and datasource is set to viewcontroller in storyboard)

@interface JobByAccountViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableViewJobList;
...
@end

My .m file

...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //realJoblist is my data array
    if (tableView == _tableViewJobList) {
        return [realJobList count];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _tableViewJobList) {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableViewJobList) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        //each cell has a tableview
        UITableView *subTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        subTableView.delegate = self;
        subTableView.dataSource = self;

        subTableView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
        [cell addSubview:subTableView];

        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableViewJobList) {
        NSLog(@"outer tableview");
    } else {
        NSLog(@"inner tableview");
    }
    ...
}

... 

In heightForRowAtIndexPath shouldn't the print statement trigger in the else block because those are the tableviews I created programmatically?

cheeezy
  • 1
  • 4
  • Where do you set tableViewJobList's dataSource and delegate to self? – Dare Jul 05 '18 at 17:08
  • @Dare I did it in the storyboard – cheeezy Jul 05 '18 at 17:10
  • 2
    Lots of problems. 1. Cells get reused. You will keep adding more and more table views to each cell. 2. You create zero height table views inside each cell. Those won't ever display anything. Why bother with table views inside cells? It's a bad idea. Why not use just the one main table view and put your data in different sections? – rmaddy Jul 05 '18 at 17:19
  • This might help: https://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell – koen Jul 05 '18 at 17:35
  • Also `cellForRowAtIndexPath:` is an *instance* method of `UITableView`; it is not a delegate method. – James Bucanek Jul 05 '18 at 18:05
  • 1
    where is else part in 'numberOfRowsInSection' ?? – Bijender Singh Shekhawat Jul 05 '18 at 18:20

1 Answers1

0

Try this,,, you just replace your table outlet name.

- (void)viewDidLoad {
    [super viewDidLoad];

        tableViewJobList.delegate = self;
            tableViewJobList.dataSource = self;

}


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        //realJoblist is my data array
        if (tableView == _tableViewJobList) {
            return [realJobList count];
        }
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (tableView == _tableViewJobList) {
            return 1;
        }
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (tableView == _tableViewJobList) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
            }

            //each cell has a tableview
            UITableView *subTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
            tableViewJobList.delegate = self;
            tableViewJobList.dataSource = self;

            subTableView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
            [cell addSubview:subTableView];

            return cell;
        }
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (tableView == _tableViewJobList) {
            NSLog(@"outer tableview");
        } else {
            NSLog(@"inner tableview");
        }
        ...
    }

    ... `enter code here`
Khushal iOS
  • 303
  • 2
  • 12