UItableView嵌套UICollectionView

首先咱們須要繼承一下UITableView而且遵照<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>的代理實現響應的代理方法,post

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {}atom

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}代理

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{}blog

-(MedCView *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}繼承

^    /*接口

|   下方這個代理方法,在通常開發中並不常見,可是這裏不可獲取ci

|    */開發

|   -(void)tableView:(UITableView *)tableView willDisplayCell:(MedCView *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{}it

|io

|  <- 箭頭指向的自定義tableView相信你也必定注意到了,這裏想要實現嵌套就須要自定義,自定義的tableview須要實現同時實現繼承UItableview和UICollectionview

這就須要實現兩個接口

//方便複製:.h的定義接口

#import <UIKit/UIKit.h>

@interface MedColView : UICollectionView

@property (nonatomic, strong) NSIndexPath *indexPath;

@end

static NSString *MedCViewCellIdentifier = @"MedCViewCellIdentifier";

@interface MedCView : UITableViewCell

@property (nonatomic, strong) MedColView *collectionView;

-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath ;

@end

//.m方法的實現

#import "MedCView.h"

#import "MedCollView.h"

#import "define.h"

@implementation MedColView

 @end

@interface MedCView()

@property (nonatomic,strong)UICollectionViewFlowLayout *layout;

@end

static NSString *CollectionViewCell = @"CollectionViewCell";

float H;

@implementation MedCView

 

-(UICollectionViewFlowLayout *)layout{

    if (_layout == nil) {

        _layout = [[UICollectionViewFlowLayout alloc] init];

        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

        _layout.itemSize = CGSizeMake(ZCScreenWidth /4,95/H);

        _layout.minimumLineSpacing = 0.1;

        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    }

    return _layout;

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;

    H =[UIView hightsize];

    self.collectionView = [[MedColView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:self.layout];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCell];

    self.collectionView.backgroundColor = [UIColor whiteColor];

    self.collectionView.showsHorizontalScrollIndicator = NO;

    self.collectionView.showsVerticalScrollIndicator = NO;

    [self addSubview:self.collectionView];

    self.collectionView.scrollEnabled = YES;

    self.collectionView.scrollsToTop = YES;

    return self;

}

 

-(void)layoutSubviews

{

    [super layoutSubviews];

    self.collectionView.frame = CGRectMake(0, self.contentView.bounds.origin.y, self.contentView.bounds.size.width , self.contentView.bounds.size.height);

}

-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath

{    

    self.collectionView.dataSource = dataSourceDelegate;

    self.collectionView.delegate = dataSourceDelegate;

    self.collectionView.indexPath = indexPath;

    [self.collectionView setContentOffset:self.collectionView.contentOffset animated:NO];

        [self.collectionView reloadData];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"sethight" object:nil];

}

@end

//setCollectionViewDataSourceDelegate:方法是爲了實現代理方法的傳遞與前方的-(void)tableView:(UITableView *)tableView willDisplayCell:(MedCView *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{}方法呼應

 

//因爲這只是個人一個Controller裏的一個模塊,因此發了一個通知去更新UI,這裏的難度就是同時繼承兩個控件實現各自的代理方法以及各種之間的關聯度也很高

相關文章
相關標籤/搜索