Comment faire une vue de sous-table dans un TableViewCell

J'ai un TableView affichant une list de domaine:

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.tableView.dataSource = mainClass.domainList; } 

La list de domaines est configurée comme ça:

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.domainList count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] init]; cell.textLabel.text = [[self.domainList objectAtIndex:indexPath.row] name]; return cell; } 

Cela fonctionne parfaitement, il affiche chaque domaine dans une rangée de ma vue de table.

Maintenant, je voudrais append un "Sub TableView" dans chaque cellule de ma vue Table pour afficher une list de documents liés au domaine. J'ai essayé ça:

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableView *table = [[UITableView alloc] init]; table.dataSource = [self.domainList objectAtIndex:indexPath.row]; UITableViewCell *cell = [[UITableViewCell alloc] init]; cell.textLabel.text = [[self.domainList objectAtIndex:indexPath.row] name]; [cell.contentView addSubView table] return cell; } 

Ça ne plante pas mais ça ne marche pas non plus. Je veux dire que la sous-list n'apparaît nulle part. Qu'est-ce que je fais mal?

datasource doit être une class implémentant le protocole UITableViewDataSource . Il semble que vous le définissiez sur un object personnalisé. Créez une class distincte avec le code que vous avez utilisé pour implémenter la première table, puis définissez la sous-list en tant que donnée source. Dans l'article objc.io " clean table view code ", ils expliquent comment créer des sources de données réutilisables. Ou vous pouvez simplement essayer par vous-même.

Considérez ce code:

 // ARRAYDATASOURCE.H #import <Foundation/Foundation.h> typedef void (^TableViewCellConfigureBlock)(id cell, id item); @interface ArrayDataSource : NSObject <UITableViewDataSource> -(id) init __atsortingbute__((unavailable("disabled, try initWithItems:cellIdentifier:configureCellBlock"))); - (id) initWithItems:(NSArray *)anItems cellIdentifier:(NSSsortingng *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock; - (id)itemAtIndexPath:(NSIndexPath *)indexPath; @end // ARRAYDATASOURCE.M #import "ArrayDataSource.h" @interface ArrayDataSource () @property (nonatomic, strong) NSArray *items; @property (nonatomic, copy) NSSsortingng *cellIdentifier; @property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock; @end @implementation ArrayDataSource - (id)initWithItems:(NSArray *)anItems cellIdentifier:(NSSsortingng *)aCellIdentifier configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock { self = [super init]; if (self) { self.items = anItems; self.cellIdentifier = aCellIdentifier; self.configureCellBlock = [aConfigureCellBlock copy]; } return self; } - (id)itemAtIndexPath:(NSIndexPath *)indexPath { return self.items[(NSUInteger) indexPath.row]; } #pragma mark UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.items.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath]; id item = [self itemAtIndexPath:indexPath]; self.configureCellBlock(cell, item); return cell; } @end