iOS TableView 的上下左右滑动

在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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,980评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,422评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,130评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,553评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,408评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,326评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,720评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,373评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,678评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,722评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,486评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,335评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,738评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,283评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,692评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,893评论 2 335

推荐阅读更多精彩内容