UItableView嵌套UICollectionView

时间:2023-03-09 03:52:16
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{}

|UItableView嵌套UICollectionView

|  <- 箭头指向的自定义tableView相信你也一定注意到了,这里想要实现嵌套就需要自定义,自定义的tableview需要实现同时实现继承UItableview和UICollectionview

这就需要实现两个接口

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方法的实现

UItableView嵌套UICollectionView

#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{}方法呼应

UItableView嵌套UICollectionView

//由于这只是我的一个Controller里的一个模块,所以发了一个通知去更新UI,这里的难度就是同时继承两个控件实现各自的代理方法以及各类之间的关联度也很高