简体   繁体   中英

UItableview inside antother UItableviewCell

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. 在此处输入图片说明

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:

  1. Subclass UITableViewCell ("MainTableViewCell") and extend it with UITableViewDelegate and UITableViewDatasource.

  2. 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; } 
  3. Subclass UITableViewCell another time: "FilmTableViewCell"

  4. 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 } 
  5. 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!

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