TableView和Scrollview的混用
刚开始做ios,所以很多都不懂,把不会的记下来,可以帮助有需要的人,也可以留下自己学习路上的点滴!!!!
首先:在一个VC里添加一个ScrollView,然后,在ScrollView里添加3个VC:(TableView、Tableview、ViewController);
第一步:创建三个不同的VC;
第二步:在viewcontroller.h里:
#import "CommunityOneViewController.h"
#import "CommunityTwoViewController.h"
#import "CommunityThreeViewController.h"
@interface MainViewController : BaseViewController<UIScrollViewDelegate>
//通过视图来定义属性
@property(nonatomic,strong) CommunityOneViewController *oneTableView;
@property(nonatomic,strong) CommunityTwoViewController *twoTableView;
@property(nonatomic,strong) CommunityThreeViewController *threeView;
//定义滚动视图
@property(nonatomic,strong) UIScrollView *scroll;
//定义分页控件
@property(nonatomic,strong) UIPageControl *page;
第三步:在viewcontroller.m里:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_scroll.pagingEnabled=YES;
_scroll.delegate = self;
_scroll.bounces=YES;
[_scroll setBackgroundColor:[UIColor lightGrayColor]];
self.scroll.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);
////////////////////////////////第一个视图////////////////////////////////////////
_oneTableView = [[CommunityOneViewController alloc]init];
_oneTableView.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
////////////////////////////////第二个视图////////////////////////////////////////
_twoTableView = [[CommunityTwoViewController alloc]init];
_twoTableView.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
////////////////////////////////第三个视图////////////////////////////////////////
_threeView = [[CommunityThreeViewController alloc]init];
_threeView.view.frame = CGRectMake(self.view.frame.size.width*2, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.scroll addSubview:_oneTableView.view];
[self.scroll addSubview:_twoTableView.view];
[self.scroll addSubview:_threeView.view];
[self.view addSubview:_scroll];
self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(150, 360, 10, 25)];
[self.page setTintColor:[UIColor redColor]];
_page.numberOfPages=3;
_page.currentPageIndicatorTintColor=[UIColor greenColor];
[self.page addTarget:self action:@selector(gotoPage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_page];
}
-(void)gotoPage{
CGRect frame;
frame.origin.x = self.scroll.frame.size.width * self.page.currentPage;
frame.origin.y = 0;
frame.size = self.scroll.frame.size;
[self.scroll scrollRectToVisible:frame animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat pageWidth = self.scroll.frame.size.width;
// 在滚动超过页面宽度的50%的时候,切换到新的页面
// + 1 是因为paged control 是从1开始计数的
int page = floor((self.scroll.contentOffset.x - pageWidth/2)/pageWidth) + 1;
self.page.currentPage = page;
}
只需要这几步,就可以在一个ScrollView里添加几个不同的视图了,然后就去执行不同的操作就可以了。