UItableView嵌套UICollectionView


首先我們需要繼承一下UITableView並且遵守<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>的代理實現響應的代理方法,

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

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

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

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

^    /*

|   下方這個代理方法,在一般開發中並不常見,但是這里不可獲取

|    */

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

|

|  <- 箭頭指向的自定義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,這里的難度就是同時繼承兩個控件實現各自的代理方法以及各類之間的關聯度也很高


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM