首先创建4个控制器oneViewController,twoViewController,threeViewController,foureViewController 并继承与UIViewController
#import "twoViewController.h"
#import "firstViewController.h"
#import "secondViewController.h"
#define HCWidth self.view.frame.size.width
#define HCHeigh self.view.frame.size.height
@interface twoViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate , UICollectionViewDataSource>{
NSArray*arrimG;
}
@property(nonatomic,strong)UITableView *tbv;
@end
staticNSString*reuseCell =@"123";
@implementationtwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
// 默认为关闭大标题模式
self.navigationController.navigationBar.prefersLargeTitles = YES;
self.navigationItem.title = @"健康数据";
[self.viewaddSubview:self.tbv];
[self createTableViewHeader];
}
-(UITableView *)tbv{
if(!_tbv) {
_tbv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
}
_tbv.dataSource = self;
_tbv.delegate=self;
return _tbv;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return 6;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
_tbv.rowHeight = 50;
NSArray *array = @[@"健康记录",@"身体测量",@"生殖健康",@"数据结果",@"心脏",@"主要体征"];
NSArray *arrimg = @[@"健康记录",@"身体测量",@"生殖健康",@"数据结果",@"心脏",@"主要体征"];
UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
imgV.image= [UIImageimageNamed:arrimg[indexPath.row]];
[celladdSubview:imgV];
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(50,10,100,30)];
label.text= array[indexPath.row];
[celladdSubview:label];
cell.detailTextLabel.text = @">";
returncell;
}
//分区个数 (几组)
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView{
return 1;
}
//每个分区有几个item (小格子的个数)
-(NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section{
return 4;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
//创建重用标识符
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseCell forIndexPath:indexPath];
//设置格子的背景颜色
cell.backgroundColor = [UIColor redColor];
//初始化图片框
UIImageView*imgV = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,180,180)];
//添加图片
imgV.image= [UIImageimageNamed:arrimG[indexPath.row]];
//添加到网格里面
[celladdSubview:imgV];
returncell;
}
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.item==0) {
firstViewController *p = [firstViewController new];
[self.navigationController pushViewController:p animated:YES];
}elseif(indexPath.item==1){
NSLog(@"正念训练");
}elseif(indexPath.item==2){
NSLog(@"营养摄入");
}else{
secondViewController *pppp = [secondViewController new];
[self.navigationController pushViewController:pppp animated:YES];
}
}
-(void)createTableViewHeader{
UIView* headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,455)];
headerView.backgroundColor= [UIColorcolorWithRed:248/255.0green:248/255.0blue:248/255.0alpha:1];
UISearchBar *search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
//search.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1];
search.barStyle = UISearchBarStyleDefault;
search.placeholder=@"搜索";
[headerViewaddSubview:search];
//创建流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//格子的大小
layout.itemSize=CGSizeMake(180,180);
//行间距
layout.minimumLineSpacing = 15;
//列间距
layout.minimumInteritemSpacing = 15;
//分区间距
layout.sectionInset=UIEdgeInsetsMake(15,15,15,15);
//
//网格视图 (表格 -> 需要注册,需要创建布局)
//1.frame
UICollectionView *clV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 405) collectionViewLayout:layout];
//2.数据源和代理
clV.delegate=self;
clV.dataSource=self;
//注册网格cell
[clVregisterClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseCell];
//给数组赋值
arrimG = @[@"1",@"2",@"3",@"4"];
//设置视图的背景颜色
clV.backgroundColor = [UIColor whiteColor];
[headerViewaddSubview:clV];
self.tbv.tableHeaderView= headerView;
}
@end
在view创建firstViewController继承UIViewController
secondViewController继承UIViewController
pTableViewCell继承UITableViewCell
在第一个界面里写上
#import "firstViewController.h"
#import "pTableViewCell.h"
@interface firstViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tbv;
@end
@implementationfirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
// 默认为关闭大标题模式
self.navigationController.navigationBar.prefersLargeTitles = YES;
self.navigationItem.title = @"健康记录";
[self.viewaddSubview:self.tbv];
}
-(UITableView *)tbv{
if(!_tbv) {
_tbv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
}
_tbv.delegate=self;
_tbv.dataSource = self;
[_tbv registerNib:[UINib nibWithNibName:@"pTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
return _tbv;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
if(section ==0) {
return1;
}else{
return3;
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.section==0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
_tbv.rowHeight=200;
UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, 180)];
imgV.image= [UIImageimageNamed:@"11"];
[celladdSubview:imgV];
returncell;
}else{
_tbv.rowHeight=150;
pTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
returncell;
}
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
if(section ==0) {
return @"少做多动,适度锻炼";
}
else{
return@"今天";
}
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.section==0) {
return180;
}
else{
return80;
}
}
@end
在第二个界面
#import "secondViewController.h"
#import "pTableViewCell.h"
@interface secondViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic , strong) UITableView *tbv;
@end
@implementationsecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
// 默认为关闭大标题模式
self.navigationController.navigationBar.prefersLargeTitles = YES;
self.navigationItem.title = @"健康记录";
[self.viewaddSubview:self.tbv];
}
-(UITableView *)tbv{
if(!_tbv) {
_tbv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
}
_tbv.delegate=self;
_tbv.dataSource = self;
[_tbv registerNib:[UINib nibWithNibName:@"pTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
return _tbv;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
if(section ==0) {
return1;
}else{
return3;
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.section==0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
_tbv.rowHeight=200;
UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, 180)];
imgV.image= [UIImageimageNamed:@"22"];
[celladdSubview:imgV];
returncell;
}else{
pTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
returncell;
}
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
if(section ==0) {
return @"到点就寝,按时起床,持之以恒";
}
else{
return@"更早";
}
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.section==0) {
return180;
}
else{
return80;
}
}
@end