Objective c 每个tableview行下的自定义Collectionview
我是c目标的新手。我想创建一个类似android应用商店的布局设计。这意味着,我有一个tableview,每一行tableview都有一个collectionview,还有一件事,我正在使用xcode7,我想用故事板创建一个设计。当我应该创建这种类型的设计时,每个tableview行都显示collectionview,但我的问题是每个collectionview都有保存数据。但我想在每一行中输入不同的数据。我扔了很多教程,但我不明白。谁能帮助我,如何创建这种类型的设计?现在,我在viewcontroller.m文件中创建一个tableview数据源和委托方法,并在自定义tableview cells.m类中使用集合视图的数据源和委托方法。请任何人帮助我 每个collectionview都有保存的数据。但我想在每一行中输入不同的数据 因为集合视图在tableViewCell中的dataSource和delegate方法相同。在tableViewCell中确定dataSource和delegate实现可能不合适,您可以将其委托给ViewController,并且每个tableViewCell都有不同的实现 我已经尝试实现了一个演示,它可以工作,但我认为可能有更好的方法。这是我的密码 细胞hObjective c 每个tableview行下的自定义Collectionview,objective-c,iphone,xcode7,Objective C,Iphone,Xcode7,我是c目标的新手。我想创建一个类似android应用商店的布局设计。这意味着,我有一个tableview,每一行tableview都有一个collectionview,还有一件事,我正在使用xcode7,我想用故事板创建一个设计。当我应该创建这种类型的设计时,每个tableview行都显示collectionview,但我的问题是每个collectionview都有保存数据。但我想在每一行中输入不同的数据。我扔了很多教程,但我不明白。谁能帮助我,如何创建这种类型的设计?现在,我在viewcont
//Cell.h
#import <UIKit/UIKit.h>
@class MTTableViewCell;
@protocol MTTableViewCellDelegate <NSObject>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section InTableViewCell:(MTTableViewCell *)cell;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath InTableViewCell:(MTTableViewCell *)cell;
@end
@interface MTTableViewCell : UITableViewCell<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
@property (nonatomic, weak) id<MTTableViewCellDelegate> delegate;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) NSInteger index; // [tablview indexPathForCell:] not work before cell is render
@end
ViewController.m
#import "ViewController.h"
#import "MTTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,MTTableViewCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 72.0;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MTTableViewCell *cell = (MTTableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.index = indexPath.row;
cell.delegate = self;
[cell.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CCell"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:( NSIndexPath *)indexPath {
MTTableViewCell *mtCell = (MTTableViewCell *)cell;
if (!mtCell.collectionView.superview) {
[mtCell addSubview:mtCell.collectionView];
mtCell.collectionView.delegate = mtCell;
mtCell.collectionView.dataSource = mtCell;
}
}
#pragma mark - MTTableViewCellDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath InTableViewCell:(MTTableViewCell *)cell {
UICollectionViewCell *cCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CCell" forIndexPath:indexPath];
cCell.backgroundColor = [UIColor redColor];
return cCell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section InTableViewCell:(MTTableViewCell *)cell {
[_tableView indexPathForCell:cell];
return cell.index
;
}
@end
#导入“ViewController.h”
#导入“MTTableViewCell.h”
@界面视图控制器()
@属性(非原子,强)UITableView*tableView;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
_tableView=[[UITableView alloc]initWithFrame:self.view.bounds样式:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.rowHeight=72.0;
[self.view addSubview:_tableView];
}
#pragma标记-UITableViewDataSource和UITableViewDelegate
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
MTTableViewCell*单元格=(MTTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@“单元格];
如果(!单元格){
cell=[[MTTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];
}
cell.index=indexPath.row;
cell.delegate=self;
[cell.collectionView注册表类:[UICollectionViewCell类]forCellWithReuseIdentifier:@“CCell”];
返回单元;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回5;
}
-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(NSIndexPath*)indexPath{
MTTableViewCell*mtCell=(MTTableViewCell*)单元;
如果(!mtCell.collectionView.superview){
[mtCell addSubview:mtCell.collectionView];
mtCell.collectionView.delegate=mtCell;
mtCell.collectionView.dataSource=mtCell;
}
}
#pragma标记-MTTableViewCellDelegate
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView单元格ForItemAtIndexPath:(NSIndexPath*)indexPath InTableViewCell:(MTTableViewCell*)单元格{
UICollectionViewCell*cCell=[collectionView dequeueReusableCellWithReuseIdentifier:@“cCell”forIndexPath:indexPath];
cCell.backgroundColor=[UIColor redColor];
返回cCell;
}
-(NSInteger)collectionView:(UICollectionView*)collectionView项目编号截面:(NSInteger)截面InTableViewCell:(MTTableViewCell*)单元{
[_tableviewindexpathforcell:cell];
返回单元格索引
;
}
@结束
希望这能帮助你:)我认为你的方法是对的。但我可以在那里创建collectionview设计。你的想法是对的。你能解释一下morehi Abhinaba~你可能会注意到我已经在
-tableView:willDisplayCell:forRowAtIndexPath:
delegate方法中设置了collectionView的委托和数据源,因为每个collectionView都与每个tableViewCell相关,所以我们需要tableViewCell已经在使用(相关的委托和数据源方法由系统调用)。我刚刚试过,你也可以用这种方法更新你的collectionview设计
#import "ViewController.h"
#import "MTTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,MTTableViewCellDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = 72.0;
[self.view addSubview:_tableView];
}
#pragma mark - UITableViewDataSource & UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MTTableViewCell *cell = (MTTableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[MTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.index = indexPath.row;
cell.delegate = self;
[cell.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CCell"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:( NSIndexPath *)indexPath {
MTTableViewCell *mtCell = (MTTableViewCell *)cell;
if (!mtCell.collectionView.superview) {
[mtCell addSubview:mtCell.collectionView];
mtCell.collectionView.delegate = mtCell;
mtCell.collectionView.dataSource = mtCell;
}
}
#pragma mark - MTTableViewCellDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath InTableViewCell:(MTTableViewCell *)cell {
UICollectionViewCell *cCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CCell" forIndexPath:indexPath];
cCell.backgroundColor = [UIColor redColor];
return cCell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section InTableViewCell:(MTTableViewCell *)cell {
[_tableView indexPathForCell:cell];
return cell.index
;
}
@end