简体   繁体   English

UITableViewCell 内 UICollectionView 的 Obj-C- 数据源?

[英]Obj-C- Datasource for UICollectionView inside of UITableViewCell?

I have a TableView (self.tableView) inside of a ViewController (DashboardViewController).我在 ViewController (DashboardViewController) 中有一个 TableView (self.tableView)。 Inside of this tableview, I have a custom cell that contains a UICollectionView.在这个 tableview 里面,我有一个包含 UICollectionView 的自定义单元格。

Everything works fine, however in order to populate the UICollectionView, I'm currently calling data inside my UITableViewCell, which I don't want to do, as every time I scroll up and down, data is called.一切正常,但是为了填充 UICollectionView,我目前正在我的 UITableViewCell 中调用数据,我不想这样做,因为每次向上和向下滚动时,都会调用数据。

Is it possible to make the UICollectionView's datasource an NSMutableArray inside DashboardViewController, instead of having to populate the NSMutableArray inside the UITableViewCell in which it is sitting?是否可以使 UICollectionView 的数据源成为 DashboardViewController 内的 NSMutableArray,而不必在它所在的 UITableViewCell 内填充 NSMutableArray?

My setup right now:我现在的设置:

UITableViewCell.h UITableViewCell.h

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray *clientsWeek;

UITableViewCell.m UITableViewCell.m

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
     [self.collectionView registerNib:[UINib nibWithNibName:@"ClientCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ClientCollectionViewCell"];

     [_collectionView setDataSource:self];
     [_collectionView setDelegate:self];

    
    NSMutableDictionary *viewParams1 = [NSMutableDictionary new];
    [viewParams1 setValue:@"cdata" forKey:@"view_name"];
    [DIOSView viewGet:viewParams1 success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        self.clientsWeek = [responseObject mutableCopy];
        NSLog(@"The people are here %@", self.clientsWeek);
        

        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", [error localizedDescription]);
    }];
    
}

Under the premise that the CellReuseIdentifier is set correctly.CellReuseIdentifier设置正确的前提下。

Of course, you can choose to get all the data in DashboardViewController , including the datasource of UICollectionView .当然,你可以选择获取DashboardViewController所有数据,包括UICollectionView的数据源。 Here you need to combine the data.这里需要合并数据。 The data source of the tableview must be an array, so the data source of UICollectionView can be placed in the array. tableview的数据源必须是数组,所以UICollectionView的数据源可以放在数组中。 Like this像这样

[
  {
   "UITableViewCellData":{},
   "UICollectionViewData":{}
  },
...
]

Of course you can also use data model objects to replace.当然你也可以用数据模型对象来替换。

Then you need to expose a method for receiving the data source to the cell, in which you can refresh your UICollectionView.然后你需要暴露一个接收数据源的方法给cell,在这个方法中你可以刷新你的UICollectionView。

UITableViewCell.h UITableViewCell.h

- (void)cellWithDataSource:(NSDictionary *)dataSource;

UITableViewCell.m UITableViewCell.m

- (void)cellWithDataSource:(NSDictionary *)dataSource{
  NSDictionary *tableViewDataSource = dataSource[@"UITableViewCellData"];
  NSDictionary *collectionViewDataSource = dataSource[@"UICollectionViewData"];
  ...
}

DashboardViewController passes data to cell DashboardViewController将数据传递给单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
    // first obtains the datasource of each cell, and passes it to each cell
    [cell cellWithDataSource:datasource];
    return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM