UITableViewDataSource’s cellForRowAtIndexPath
For a certain row, use your custom cell that has the tableview.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row != 0) { NSLog(@"%s", __FUNCTION__); UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"tvcItems"]; if(cell==nil) { NSLog(@"%s - Creating new cell for index %lu", __FUNCTION__, indexPath.row); cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"tvcItems"]; } [cell.textLabel setText:[self.myData objectAtIndex:[indexPath row]]]; return cell; } else { // for row 0, we create another table inside the cell NSLog(@"%s - Creating MyTableViewCell for cell # %lu", __FUNCTION__, indexPath.row); static NSString *cellIdentifier = @"HistoryCell"; // Similar to UITableViewCell, but MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } // Just want to test, so I hardcode the data //cell.descriptionLabel.text = @"Testing"; return cell; } } |
UITableViewDelegate’s heightForRowAtIndexPath
Make sure your table view delegate method heightForRowAtIndexPath is appropriate so people can see your custom cell.
1 2 3 4 5 |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row==0) { return 300.0f; } else { return 86.0f; |
The cell that contains the inner table
Create your tableview with its associated data array, then add it to the cell’s view hierarchy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#import "MyTableViewCell.h" @implementation MyTableViewCell @synthesize dataArray; //array to hold submenu data - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self setBackgroundColor:[UIColor orangeColor]]; self.frame = CGRectMake(0.0f, 20.0f, self.bounds.size.width, 300.0f); self.dataArray = [[NSMutableArray alloc] initWithObjects:@"Inner Table - One", nil]; UITableView * subMenuTableView = [[UITableView alloc]initWithFrame:CGRectMake(0.0f, 20.0f, self.bounds.size.width, 280.0f) style:UITableViewStylePlain]; //create tableview a subMenuTableView.tag = 100; subMenuTableView.delegate = self; subMenuTableView.dataSource = self; subMenuTableView.alwaysBounceVertical = NO; [self addSubview:subMenuTableView]; // add it cell ... ... } .. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"]; if(cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"]; } cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row]; return cell; } ... |