2017-05-26 54 views
0

我是新的目标c。我想创建一个像android应用程序商店一样的布局设计。这意味着,我有一个tableview和tableview的每一行有一个collectionview,还有一件事,我使用xcode7,我想创建一个故事板的设计。当我应该创建一个这样的设计类型的每个tableview行显示collectionview但我的问题是,每个collectionview有保存数据。但我想要在每一行中使用不同的数据。我去扔很多教程,但我不明白。任何人都可以帮助我,如何创建这种类型的设计?现在我在viewcontroller.m文件中创建tableview数据库和委托方法,并将集合视图的datasourse和委托方法用于自定义tableview cells.m类。请任何一位协助我在每个tableview行下自定义Collectionview

回答

0

每个collectionview都有保存数据。但我想不同的数据中的每一行

因为tableViewCell您的收藏视图的datasourse和委托方法same.maybe它不是确定以确定数据源和tableViewCell委托执行中,你可以委托它到你的ViewController,并且每个tableViewCell都有不同的实现。

我试图执行演示,它可以工作,但我认为可能有更好的方法。这是我的代码

Cell.h

//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 

Cell.m

//Cell.m 
#import "MTTableViewCell.h" 

@interface MTTableViewCell() 

@end 

@implementation MTTableViewCell 


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 
     _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout]; 
    } 
    return self; 
} 

#pragma mark - 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    if (_delegate && [_delegate respondsToSelector:@selector( collectionView:numberOfItemsInSection:InTableViewCell:)]) { 
     return [_delegate collectionView:collectionView numberOfItemsInSection:section InTableViewCell:self]; 
    } 
    return 0; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath { 
    if (_delegate && [_delegate respondsToSelector:@selector( collectionView:cellForItemAtIndexPath:InTableViewCell:)]) { 
     return [_delegate collectionView:collectionView cellForItemAtIndexPath:indexPath  InTableViewCell:self]; 
    } 
    return nil; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    return CGSizeMake(50, 50); 
} 

@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 

希望这可以帮助你:)

+0

我想你的方式是对的。但是,我可以创建一个collectionview设计。你的协议想法是对的。您可以解释更多 – Abhinaba

+0

您好Abhinaba〜您可能会注意到我已经在'-tableView:willDisplayCell:forRowAtIndexPath:'委托方法中设置了collectionView的委托和数据源,因为每个collectionView都与每个tableViewCell相关,所以我们需要tableViewCell已经可以使用(相关的委托和数据源方法被系统调用)。我只是试过了,你也可以用这个方法更新你的collectionview的设计。 :) – LiMengtian

相关问题