In one of the cell of a tableView, i want to put another tableView(toyTable for example). Can anyone help me in this? I have created a tableView (main one) and another tableview in my project. Now in my main tableView, i have many cells and in one of the cell, i want that another table(toyTable) to be present in the cell.
Asked
Active
Viewed 945 times
2
-
why you need inside of another table. any special reason for that – codercat Feb 20 '14 at 09:01
-
what you try to achieving – codercat Feb 20 '14 at 09:02
-
@santhu: Can you please show me the pic of that output that of that answer which is ticked correct in the link you posted? please.. i need it badly. – user3202087 Feb 20 '14 at 12:31
4 Answers
1
Putting table view inside table cell is a bad design. Insert toyTable
as a section of main table view instead.

vokilam
- 10,153
- 3
- 45
- 56
-
Why is it better to add a 2nd table view as a section instead as a child of a contentview? are you thinking of additional features like section collapsing? – Jano Feb 20 '14 at 09:19
-
@Jano I guess, there will be issues with reusing cells of inner table view. Collapsing could be a responsibility of a table data source. – vokilam Feb 20 '14 at 09:25
1
Lets say you have two tables.
Make properties for both of them
// This one might be connected as an outlet with your storyboard
@property (nonatomic) IBOutlet UITableViewController *mainTable;
// This one should be in the .m file
@property (nonatomic) UITableViewController *toyTable;
Implement delegate and datasource protocol methods for each.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.mainTable) {
return 2;
} else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.mainTable) {
return 10;
} else {
return 5;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is the indexPath for the cell that should contain
// the nested table view, initialize your self.toyTable, set
// datasource and delegate etc.
// Else configure your default outer cell
} else {
// Configure the cells of your self.toyTable;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is any normal outer cell
return UITableViewAutomaticDimension;
// If this is the outer cell that contains self.toyTable
return some bigger value (if desired)
} else {
// The height of the inner cell
return UITableViewAutomaticDimension;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.mainTable) {
// If this is the outer cell that contains self.toyTable
// You should ignore the selection
// Else handle it according to your needs
} else {
// Handle click on inner cell
}
}
If you are using a static table view as your mainTable, you can directly drag a table view onto one of your static cells and define prototype cells etc for it.

Marc
- 6,051
- 5
- 26
- 56
0
Why are u doing this because its a bad idea for nesting the tableview instead UITableview already having expand and collapsing property. Use below link for reference http://www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/
0
In cellForRowAtIndexPath add that table and do the code like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == mainTable) {
//do this for main table
}
else {
//do this for second table
}
}

Samkit Jain
- 2,523
- 16
- 33