I want a tableview inside another tableviewCell like the following image.It shows one complete cell with a few details and a tableview. How can i do this?I was following this link Link.This is an old code .It is using xibs.I dont have any idea where to set the delegate for the inner tableview.Please help.Any suggestion will be realy helpfull.

- 33
- 5
2 Answers
Check my answer in this ios 8 Swift - TableView with embedded CollectionView. You have replace that UICollectionView with UITableView.
Everything else is pretty much the same. Its just a head start with UITableView and UICollectionView created programmatically.
I can change it accordingly if you don't understand.
My first idea would be:
Subclass UITableViewCell ("MainTableViewCell") and extend it with UITableViewDelegate and UITableViewDatasource.
Next to all the properties you need in "MainTableViewCell" add a TableView "tableViewFilms" and an array "films" for the Films. Also don't forget to add the datasource methods for a tableview to the implementation file. To easily setup a cell I add a setup-method to the header-file. Which can be called once the cell is instantiated. You can modify it as you want, give it as many parameters as you want and in the implementation (see step 4) set datasource and delegate of your inner tableview.
- (void)setupCellWithDictionary:(NSDictionary *)dict AndArray:(NSArray *)filmsForInnerTable;
You can call this method in your datasource method, once a cell is instantiated:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainTableViewCell" forIndexPath:indexPath]; NSDictionary *dict = (NSDictionary *) allDataDictionaries[indexPath.row]; [cell setupCellWithDictionary:dict AndArray:filmsForInnerTable]; return cell; }
Subclass UITableViewCell another time: "FilmTableViewCell"
When setting up the a Cell of "MainTableViewCell", set the delegate and the datasource of "tableViewFilms" to self (object of "MainTableViewCell").
- (void)setupCellWithDictionary:(NSDictionary *)dict AndArray:(NSArray *)filmsForInnerTable{ self.films = filmsForInnerTable; self.tableViewFilms.dataSource = self; self.tableViewFilms.delegate = self; [self.tableView reload]; //more instructions }
Populate the tableview with the data from the array "films" using "FilmTableViewCells".
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FilmTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FilmTableViewCell" forIndexPath:indexPath]; Film *film = (Film*)films[indexPath.row]; [cell setupCellWithFilm:film]; return cell; }
Hope this helps.
Don't forget to use Outlets, the method definitions and to set the reuse-identifiers for the cells!

- 324
- 4
- 13
-
Are you saying that the MainTableViewCell.m should have the datasource methods for the inner table?if so,how can I transfer data from mainViewController to MainTableViewCell.I have tried many methods.Is there any init methods? – iosDeveloper Jul 29 '15 at 06:31
-
i edited my answer, this should help you, and you should be able to find your way through. Sry for typos, I wrote it all here and didn't test it. – dfinki Jul 29 '15 at 07:14
-
What is the use of (NSDictionary *)dict in 4th step? – iosDeveloper Jul 29 '15 at 07:29
-
I added it, just in case you want to setup something more, but you can easily just drop it from the method definition if you don't need it. – dfinki Jul 29 '15 at 07:47