自动轮播

xiaoxiao2025-09-14  171

首先在viewcontroller中设置tabbar,然后准备在某一个主界面书写界面

#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 static NSString *reuseCell = @"123"; @implementation twoViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; // 默认为关闭大标题模式 self.navigationController.navigationBar.prefersLargeTitles = YES; self.navigationItem.title = @"健康数据"; [self.view addSubview: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 = [UIImage imageNamed:arrimg[indexPath.row]]; [cell addSubview:imgV]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 30)]; label.text = array[indexPath.row]; [cell addSubview:label]; cell.detailTextLabel.text = @">"; return cell; } //分区个数 (几组) -(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 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 180, 180)]; //添加图片 imgV.image = [UIImage imageNamed:arrimG[indexPath.row]]; //添加到网格里面 [cell addSubview:imgV]; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.item == 0) { firstViewController *p = [firstViewController new]; [self.navigationController pushViewController:p animated:YES]; }else if (indexPath.item == 1){ NSLog(@"正念训练"); }else if (indexPath.item == 2){ NSLog(@"营养摄入"); }else{ secondViewController *pppp = [secondViewController new]; [self.navigationController pushViewController:pppp animated:YES]; } } -(void)createTableViewHeader{ UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 455)]; headerView.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha: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 = @"搜索"; [headerView addSubview: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 [clV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseCell]; //给数组赋值 arrimG = @[@"1",@"2",@"3",@"4"]; //设置视图的背景颜色 clV.backgroundColor = [UIColor whiteColor]; [headerView addSubview:clV]; self.tbv.tableHeaderView = headerView; } @end

之后创建自定义cell xib和各种跳转的控制器

#import "firstViewController.h" #import "pTableViewCell.h" @interface firstViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,strong)UITableView *tbv; @end @implementation firstViewController - (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.view addSubview: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) { return 1; }else{ return 3; } } -(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 = [UIImage imageNamed:@"11"]; [cell addSubview:imgV]; return cell; }else{ _tbv.rowHeight = 150; pTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"]; return cell; } } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (section == 0) { return @"少做多动,适度锻炼"; } else{ return @"今天"; } } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section==0) { return 180; } else{ return 80; } } @end #import "secondViewController.h" #import "pTableViewCell.h" @interface secondViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic , strong) UITableView *tbv; @end @implementation secondViewController - (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.view addSubview: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) { return 1; }else{ return 3; } } -(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 = [UIImage imageNamed:@"22"]; [cell addSubview:imgV]; return cell; }else{ pTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"]; return cell; } } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (section == 0) { return @"到点就寝,按时起床,持之以恒"; } else{ return @"更早"; } } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section==0) { return 180; } else{ return 80; } } @end

// 默认为关闭大标题模式 self.navigationController.navigationBar.prefersLargeTitles = YES;

转载请注明原文地址: https://www.6miu.com/read-5036313.html

最新回复(0)