在ios开发中,特别是在做电商类的app时经常会有需求,要求tableivew的上下左右滑动,类似像闲🐟首页的效果,其实就是scrollview与tableview的嵌套,这个我相信大家都知道,那怎么实现上下左右滑动呢,有些同学就使用他们之间的联动,以及一些手势来做,确实可以实现,但体验极差,还会有停顿,性能就更差了。
而且东风要告诉大家一个事实,在实际开发中一定不能使用scrollview与tableview的嵌套来实现上下左右滑动,不要问为什么,只有这么做过的人,痛过的人才知道,好吧,不装B了。稍微解释下,如果使用scrollview与tableview的嵌套就会导致controller.m的代码太多,使代码的整体灵活性降低,不利于以后的需求变更,而且稍微数据没处理好就会导致崩溃,正确的做法是使用scrollview与controller嵌套,然后在controller中加载tableview,最后将controller的view加载到scrollview,通过代理或block再回到原来的controller中处理事件。
今天东风在这里给大家展示这种简便,轻快的方法实现。(主要是运用了tableview的headview来做文章,大家自己看吧)
当然东风这里给大家展示的只是其中一种方法,当然还有更好的方法,就等大家自己去开发吧,知识就看你怎么用了。
源码demo git地址:https://github.com/cocoaliaolei/tableView.git
首先我们先建一个工程,直接在viewcontroller.m中直接写代码(太懒了,将就吧)先把宏写好吧
#import "ViewController.h"
#import "TableViewController.h"
#import "HeadView.h"
#define WD [UIScreen mainScreen].bounds.size.width
#define HG [UIScreen mainScreen].bounds.size.height
#define count 3//三个tableview
添加成员属性
@property (nonatomic,strong)UIScrollView *scrolView;//全局scrollview使可以左右滑动
@property (nonatomic,strong)NSMutableArray *aray;//用于装scroller中的controller
@property (nonatomic,strong)HeadView *hView;//headview
先来三个懒加载(果然懒)
-(UIScrollView *)scrolView{
if (!_scrolView) {
_scrolView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WD, HG)];
_scrolView.pagingEnabled = YES;
_scrolView.bounces = NO;
_scrolView.delegate = self;
_scrolView.contentSize = CGSizeMake(count * WD, HG);
}
return _scrolView;
}
-(HeadView *)hView{
if (!_hView) {
_hView = [[HeadView alloc]initWithFrame:CGRectMake(0, 0, WD, 240)];
_hView.delegate = self;
}
return _hView;
}
-(NSMutableArray *)aray{
if (!_aray) {
_aray = [[NSMutableArray alloc]init];
}
return _aray;
}
viewdidload中创建控件
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
}
-(void)initView{
[self creatScrollView];
[self creatTableView];
[self creatHeadView];
}
-(void)creatScrollView{
[self.view addSubview:self.scrolView];
}
-(void)creatHeadView{
[self.view addSubview:self.hView];
}
-(void)creatTableView{
for (int i = 0; i < count; i ++) {
TableViewController *tbCtl = [[TableViewController alloc]init];
tbCtl.delegate = self;
[self.aray addObject:tbCtl];
tbCtl.view.frame = CGRectMake(i * WD, 0, WD, HG);
tbCtl.view.tag = 10 + i;
[self.scrolView addSubview:tbCtl.view];
}
}
TableViewController.h文件
#import "ViewController.h"
@protocol TableViewControllerDelegate <NSObject>
@optional
-(void)TableViewControllerScrollto:(CGFloat)locattionY;
@end
@interface TableViewController : UIViewController
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,weak)id<TableViewControllerDelegate>delegate;
@end
TableViewController.m文件
#import "TableViewController.h"
#define WD [UIScreen mainScreen].bounds.size.width
#define HG [UIScreen mainScreen].bounds.size.height
@interface TableViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation TableViewController
-(UITableView *)tbView{
if (!_tbView) {
_tbView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WD, HG)];
_tbView.delegate = self;
_tbView.dataSource = self;
_tbView.showsVerticalScrollIndicator = NO;
[_tbView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
return _tbView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
}
-(void)initView{
[self.view addSubview:self.tbView];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 240;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"春暖花开-%d-%d",(int)self.view.tag - 10,(int)indexPath.row];
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat f = scrollView.contentOffset.y;
if (_delegate && [_delegate respondsToSelector:@selector(TableViewControllerScrollto:)]) {
[_delegate TableViewControllerScrollto:f];
}
}
@end
HeadView.h文件
#import <UIKit/UIKit.h>
@class HeadView;
@protocol HeadViewDelegate <NSObject>
@optional
-(void)HeadViewSelectedBtutton:(NSInteger)indexs;
@end
@interface HeadView : UIView
@property (nonatomic,weak)id<HeadViewDelegate>delegate;
-(void)headViewUpdateBottomLineState:(int)indexs;
@end
HeadView.m文件
#import "HeadView.h"
@interface HeadView ()
{
UIButton *temBtn;
}
@property (nonatomic,strong)UIView *Line;
@end
@implementation HeadView
-(UIView *)Line{
if (!_Line) {
_Line = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 1, self.frame.size.width / 3, 1)];
_Line.backgroundColor = [UIColor redColor];
}
return _Line;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
[self initView];
}
return self;
}
-(void)initView{
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,self.frame.size.width, self.frame.size.height - 40)];
imgView.image = [UIImage imageNamed:@"11.jpg"];
[self addSubview:imgView];
CGFloat y = CGRectGetMaxY(imgView.frame);
[self addSubview:[self creatButton:CGRectMake(0, y, self.frame.size.width/3, 39) :@"想你的夜" :120]];
[self addSubview:[self creatButton:CGRectMake(self.frame.size.width/3, y, self.frame.size.width/3, 39) :@"多希望你" :121]];
[self addSubview:[self creatButton:CGRectMake(self.frame.size.width*2/3, y, self.frame.size.width/3, 39) :@"能在我身边" :122]];
[self addSubview:self.Line];
}
-(UIButton *)creatButton:(CGRect)frame :(NSString *)title :(NSInteger)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
btn.titleLabel.font = [UIFont systemFontOfSize:15];
if (tag == 120){
temBtn = btn;
btn.selected = YES;
}
btn.tag = tag;
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
-(void)btnClick:(UIButton *)btn{
temBtn.selected = NO;
btn.selected = YES;
temBtn = btn;
if (_delegate && [_delegate respondsToSelector:@selector(HeadViewSelectedBtutton:)]) {
[_delegate HeadViewSelectedBtutton:btn.tag - 120];
}
switch (btn.tag) {
case 120:
[self setBottomLine:0];
break;
case 121:
[self setBottomLine:self.frame.size.width / 3];
break;
case 122:
[self setBottomLine:self.frame.size.width*2 / 3];
break;
default:
break;
}
}
-(void)setBottomLine:(CGFloat)x{
CGRect rect = self.Line.frame;
rect.origin.x = x;
[UIView animateWithDuration:0.23 animations:^{
self.Line.frame = rect;
}];
}
-(void)headViewUpdateBottomLineState:(int)indexs{
temBtn.selected = NO;
UIButton *btn = (UIButton *)[self viewWithTag:120 + indexs];
btn.selected = YES;
temBtn = btn;
[self setBottomLine:self.frame.size.width *indexs / 3];
}
@end