项目需求,首页要实现一个,最底部是轮播图,上面是个tableview列表。不滑动的时候让底部轮播图响应点击和滑动事件。
1. 自定义一个listView,实现tableview1列表
#import <UIKit/UIKit.h>
#import "HeaderView.h"
#import "listView2.h"
@interface listView : UIView<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * listtableView;
@property (nonatomic,strong)listView2 * listView_2;
@end
#import "listView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@implementation listView
/*
主要实现下面这5个方法
*/
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if(hitView.tag == 7600){
self.listtableView.userInteractionEnabled = NO;
return nil;
}
self.listtableView.userInteractionEnabled = YES;
return hitView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self == [super initWithFrame:frame]) {
if (@available(iOS 11.0, *)) {
self.listtableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else {
}
self.listView_2 = [[listView2 alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
[self addSubview:self.listView_2];
[self addSubview:self.listtableView];
}
return self;
}
-(UITableView *)listtableView
{
if (!_listtableView) {
_listtableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
_listtableView.dataSource = self;
_listtableView.delegate = self;
_listtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_listtableView.backgroundColor = [UIColor clearColor];
_listtableView.showsVerticalScrollIndicator = NO;
UIView * tabheader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
tabheader.backgroundColor = [UIColor clearColor];
tabheader.tag = 7600;
_listtableView.tableHeaderView = tabheader;
_listtableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)];
[self disableEstimatedRowHeight];
}
return _listtableView;
}
-(void)disableEstimatedRowHeight
{
if (@available(iOS 15.0, *)) {
self.listtableView.sectionHeaderTopPadding = 0;
}
self.listtableView.estimatedRowHeight = 0;
self.listtableView.estimatedSectionFooterHeight = 0;
self.listtableView.estimatedSectionHeaderHeight = 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 20;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * DetTop = @"LLJTheCoach_CourseDetTopTableViewCellid";
UITableViewCell * DetTop_cell = [tableView dequeueReusableCellWithIdentifier:DetTop];
if (!DetTop_cell) {
DetTop_cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetTop];
DetTop_cell.selectionStyle = UITableViewCellSelectionStyleNone;
DetTop_cell.backgroundColor = [UIColor blueColor];
}
return DetTop_cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
view.backgroundColor = [UIColor blackColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
view.backgroundColor = [UIColor blackColor];
return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableViewdeselectRowAtIndexPath:indexPath animated:NO];
NSLog(@"tableview_2");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
2. 自定义一个listView2,实现tableview2列表
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface listView2 : UIView<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * listtableView;
@end
NS_ASSUME_NONNULL_END
#import "listView2.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@implementation listView2
- (instancetype)initWithFrame:(CGRect)frame
{
if (self == [super initWithFrame:frame]) {
[self addSubview:self.listtableView];
}
return self;
}
-(UITableView *)listtableView
{
if (!_listtableView) {
_listtableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300) style:UITableViewStyleGrouped];
_listtableView.dataSource = self;
_listtableView.delegate = self;
_listtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_listtableView.backgroundColor = [UIColor clearColor];
_listtableView.showsVerticalScrollIndicator = NO;
[self disableEstimatedRowHeight];
}
return _listtableView;
}
-(void)disableEstimatedRowHeight
{
if (@available(iOS 15.0, *)) {
self.listtableView.sectionHeaderTopPadding = 0;
}
self.listtableView.estimatedRowHeight = 0;
self.listtableView.estimatedSectionFooterHeight = 0;
self.listtableView.estimatedSectionHeaderHeight = 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 20;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * DetTop = @"LLJTheCoach_CourseDetTopTableViewCellid";
UITableViewCell * DetTop_cell = [tableView dequeueReusableCellWithIdentifier:DetTop];
if (!DetTop_cell) {
DetTop_cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetTop];
DetTop_cell.selectionStyle = UITableViewCellSelectionStyleNone;
DetTop_cell.backgroundColor = [UIColor redColor];
}
return DetTop_cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
view.backgroundColor = [UIColor yellowColor];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
view.backgroundColor = [UIColor yellowColor];
return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableViewdeselectRowAtIndexPath:indexPath animated:NO];
NSLog(@"tableview_1");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
3. vc调用
- (void)viewDidLoad {
[super viewDidLoad];
// tableview headerview覆盖 底部的tableview 让底部的tableview 相应事件
listView * list = [[listView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
[self.view addSubview:list];
}
网友评论