如何实现UICollectionView的委托和数据源方法,当它在iOS中的自定义TableViewCell中时,目标C

注意:我不想使用任何第三方库

  • 我有一个自定义table view cell (表视图工作正常)。
  • 现在在table view cell我想实现一个collection view
  • 现在我的问题是,我应该在哪里实现集合视图,这是在custom table view cell内的delegate方法和data source方法。 下面是我所尝试过的。

Tabel View Controller实现

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tvcell"]; return cell; } 

好好工作

现在在这个FirstTableViewCell里面有一个collection view

这是我的FirstTableViweCell.h文件

 #import <UIKit/UIKit.h> @interface FirstTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource> @property (weak, nonatomic, nullable) IBOutlet UICollectionView *insideCollectionView; @property (strong, nonnull,nonatomic) NSArray *mycollectionData; - (void)setColletionData :(nonnull NSArray *)collectionData; @end 

这是我的FirstTableViewCell.m文件

 #import "FirstTableViewCell.h" @implementation FirstTableViewCell - (void)awakeFromNib { [super awakeFromNib]; self.insideCollectionView.delegate = self; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (void)setColletionData:(NSArray *)collectionData { self.mycollectionData = collectionData; [self.insideCollectionView reloadData]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell" forIndexPath:indexPath]; return ccell; } @end 

以下方法

 - (void)setColletionData:(NSArray *)collectionData { self.mycollectionData = collectionData; [self.insideCollectionView reloadData]; } 

我曾经在tableview cellforrowatindexpath中为collectionView设置array

那么为DataSource Custom Table View Cell内的CollectionView实现DataSourceDelegate方法的正确方法是什么?

你有什么本质上是正确的。 唯一真正需要更改的是更新集合视图数据源和自定义单元类中的委托方法,以便正确引用self.mycollectionData

例如。 将numberOfItemsInSection更改为:

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.mycollectionData.count; } 

Horraaaaaaaay ….. = D这是完成的答案。

表视图实现.m文件

 #import "ViewController.h" #import "FirstTableViewCell.h" @interface ViewController () @end @implementation ViewController { NSArray *bbarray; } - (void)viewDidLoad { [super viewDidLoad]; bbarray = [NSArray arrayWithObjects:@"33",@"44", nil]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tvcell"]; [cell setColletionData:bbarray]; return cell; } @end 

自定义表视图单元格内的实现

自定义表格视图.h文件

 #import <UIKit/UIKit.h> @interface FirstTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource> @property (weak, nonatomic, nullable) IBOutlet UICollectionView *insideCollectionView; @property (strong, nonnull,nonatomic) NSArray *mycollectionData; - (void)setColletionData :(nonnull NSArray *)collectionData; @end 

自定义表格视图.m文件

 #import "FirstTableViewCell.h" @implementation FirstTableViewCell - (void)awakeFromNib { [super awakeFromNib]; // These two are very important. self.insideCollectionView.delegate = self; self.insideCollectionView.dataSource = self; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } //this method is used to set the data , call it in cell for row at index path in table view implementation. - (void)setColletionData:(NSArray *)collectionData { self.mycollectionData = collectionData; [self.insideCollectionView reloadData]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { NSLog(@"%lu", (unsigned long)self.mycollectionData.count); return [self.mycollectionData count]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell" forIndexPath:indexPath]; return ccell; } @end