How to make a Sub Table view in a TableViewCell

前端 未结 1 550
说谎
说谎 2021-01-26 11:57

I have a TableView displaying a list of domain :

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    self.table         


        
相关标签:
1条回答
  • 2021-01-26 12:14

    datasource must be a class implementing the protocol UITableViewDataSource. It looks like you are setting it to a custom object. Create a separate class with the code you used to implement the first table, then set the sublist as source data. In the objc.io article “clean table view code” they explain how to make reusable datasources. Or you can just give it a try on your own.

    Consider this code:

    // ARRAYDATASOURCE.H
    
    #import <Foundation/Foundation.h>
    
    typedef void (^TableViewCellConfigureBlock)(id cell, id item);
    
    @interface ArrayDataSource : NSObject <UITableViewDataSource>
    
    -(id) init __attribute__((unavailable("disabled, try initWithItems:cellIdentifier:configureCellBlock")));
    
    - (id) initWithItems:(NSArray *)anItems
          cellIdentifier:(NSString *)aCellIdentifier
      configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
    
    - (id)itemAtIndexPath:(NSIndexPath *)indexPath;
    
    @end
    
    
    // ARRAYDATASOURCE.M
    
    #import "ArrayDataSource.h"
    
    @interface ArrayDataSource ()
    @property (nonatomic, strong) NSArray *items;
    @property (nonatomic, copy) NSString *cellIdentifier;
    @property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
    @end
    
    @implementation ArrayDataSource
    
    - (id)initWithItems:(NSArray *)anItems
         cellIdentifier:(NSString *)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
    
    0 讨论(0)
提交回复
热议问题