Примечание: Я не хочу использовать какие-либо сторонние библиотекиКак реализовать делегат и DataSource методы UICollectionView, когда он находится внутри пользовательских TableViewCell в прошивке, цель C
- Я имею
tableview
с с обычайtable view cell
(рабочий стол хорошо работает). - Теперь внутри
table view cell
Я хочу реализоватьcollection view
. - Теперь моя проблема в том, где я должен реализовать методы
delegate
и методыdata source
для вида коллекции, который находится внутриcustom table view cell
. ниже - это то, что я пробовал.
реализация 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];
}
Я использовал для установки array
для collectionView
, в cellforrowatindexpath
в tableview
Так что правильный способ реализации
DataSource
иDelegate
методов дляCollectionView
, который находится внутриCustom Table View Cell
.
Вы делаете отлично. :) –