2017-05-26 5 views
0

Ich bin neu in Ziel c. Ich möchte ein Layout-Design wie Android App Store erstellen. Das heißt, ich habe eine Tabellenansicht und jede Zeile von Tableview hat eine Collectionview, und noch eine Sache, ich benutze xcode7 und ich möchte ein Design mit Storyboard erstellen. Wenn ich eine solche Art von Design erstellen sollte, zeigt jede Tabellenansichtszeile Sammlungsansicht an, aber mein Problem ist, dass jede Sammlungsansicht Daten speichern kann. aber ich möchte verschiedene Daten in jeder Zeile. Ich gehe viele Tutorials, aber ich kann nicht verstehen. Kann mir jemand helfen, wie man diese Art von Design erstellt? Jetzt bin ich eine Tabellenansicht datasourse und Delegate-Methode in viewcontroller.m Datei erstellen und auch Datensource und Delegate-Methode der Sammlungsansicht in benutzerdefinierte tableview cells.m Klasse verwenden. Bitte helfen Sie mirCustom Collectionview unter jeder Tabellenansicht Zeilen

Antwort

0

jede collectionview haben Daten speichern. aber ich möchte verschiedene Daten in jeder Zeile

weil Ihre Sammlung Ansicht des datasourse und Delegatmethode in tableViewCell ist same.maybe es nicht in Ordnung, Datenquelle zu bestimmen und delegieren Implementierung in tableViewCell, können Sie es delegieren, um Ihre Viewcontroller, und jede TableViewCell's hat unterschiedliche Implementierung.

Ich habe versucht, eine Demo zu implementieren, kann es funktionieren, aber ich denke, kann einen besseren Weg haben. dies ist mein Code

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 

Hope this kann Ihnen helfen :)

+0

Ich denke, Dein Weg ist richtig. Aber wo kann ich ein Collectionview-Design erstellen? Ihre Protokollidee ist richtig. Kann man mehr erklären – Abhinaba

+0

Hallo Abhinaba ~ Sie können feststellen, ich habe CollectionView 's Delegate und dataSource in '- TableView: WillDisplayCell: forRowAtIndexPath:' Delegate-Methode, da jedes collectionView zu jedem TableViewCell verwandt ist, so müssen wir TableViewCell bereits für den Einsatz (Die zugehörige Delegate- und dataSource-Methode wird von System aufgerufen). Und ich habe es gerade versucht, Sie können auch das Design Ihrer Kollektion in dieser Methode aktualisieren. :) – LiMengtian

Verwandte Themen