在ios開發(fā)中,特別是在做電商類的app時(shí)經(jīng)常會(huì)有需求,要求tableivew的上下左右滑動(dòng),類似像閑??首頁的效果,其實(shí)就是scrollview與tableview的嵌套,這個(gè)我相信大家都知道,那怎么實(shí)現(xiàn)上下左右滑動(dòng)呢,有些同學(xué)就使用他們之間的聯(lián)動(dòng),以及一些手勢(shì)來做,確實(shí)可以實(shí)現(xiàn),但體驗(yàn)極差,還會(huì)有停頓,性能就更差了。
而且東風(fēng)要告訴大家一個(gè)事實(shí),在實(shí)際開發(fā)中一定不能使用scrollview與tableview的嵌套來實(shí)現(xiàn)上下左右滑動(dòng),不要問為什么,只有這么做過的人,痛過的人才知道,好吧,不裝B了。稍微解釋下,如果使用scrollview與tableview的嵌套就會(huì)導(dǎo)致controller.m的代碼太多,使代碼的整體靈活性降低,不利于以后的需求變更,而且稍微數(shù)據(jù)沒處理好就會(huì)導(dǎo)致崩潰,正確的做法是使用scrollview與controller嵌套,然后在controller中加載tableview,最后將controller的view加載到scrollview,通過代理或block再回到原來的controller中處理事件。
今天東風(fēng)在這里給大家展示這種簡(jiǎn)便,輕快的方法實(shí)現(xiàn)。(主要是運(yùn)用了tableview的headview來做文章,大家自己看吧)
當(dāng)然東風(fēng)這里給大家展示的只是其中一種方法,當(dāng)然還有更好的方法,就等大家自己去開發(fā)吧,知識(shí)就看你怎么用了。
源碼demo git地址:https://github.com/cocoaliaolei/tableView.git
首先我們先建一個(gè)工程,直接在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//三個(gè)tableview
添加成員屬性
@property (nonatomic,strong)UIScrollView *scrolView;//全局scrollview使可以左右滑動(dòng)
@property (nonatomic,strong)NSMutableArray *aray;//用于裝scroller中的controller
@property (nonatomic,strong)HeadView *hView;//headview
先來三個(gè)懶加載(果然懶)
-(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中創(chuàng)建控件
- (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