表格+網格+滾動檢視自動滾動
阿新 • • 發佈:2018-12-16
四個介面 , 建立四個繼承於ViewController的類 去百度雲下載“滾動” 和 “彈窗” 點選這裡 ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UITabBarController
@end
ViewController.m介面
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createTabbar]; } -(void)createTabbar{ //控制器名稱 NSArray *array = @[@"oneViewController",@"twoViewController",@"threeViewController",@"fourViewController"]; //點選前的樣式 NSArray *unSelectedImagerArray = @[@"首頁",@"服務",@"卡券",@"個人"]; //點選後的樣式 NSArray *SelectedImagerArray = @[@"首頁",@"服務",@"卡券",@"個人"]; //控制器陣列 NSMutableArray *ViewContro = [NSMutableArray new]; //根據控制器的名稱建立響應的控制器 並新增到控制器的數組裡 for (int i = 0; i<array.count; i++) { //string ----> Controller UIViewController *vc = [[NSClassFromString(array[i]) alloc]init]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc]; //設定Tabbritem 的 選中圖片 未選中圖片 標題 nav.tabBarItem.image = [[UIImage imageNamed:unSelectedImagerArray[i] ] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; nav.tabBarItem.selectedImage = [[UIImage imageNamed:SelectedImagerArray [i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; nav.title = unSelectedImagerArray[i]; // nav.navigationBar.barTintColor = [UIColor blackColor]; nav.navigationBar.barTintColor = [UIColor colorWithRed:0/255.0 green:138/255.0 blue:247/255.0 alpha:1]; nav.navigationBar.translucent = NO; //新增到數組裡 [ViewContro addObject:nav]; } self.viewControllers = ViewContro; self.selectedIndex = 1; } @end
我們要寫的是第二個介面 所以直接在第二個介面
#import "twoViewController.h" #import "HWPopTool.h" #import "CarouseView.h" #import "CarouseViewPlus.h" @interface twoViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,CarouseViewDataSource, CarouseViewDelegate> { // 輪播圖變數,其實作為區域性變數也行 CarouseView *carouseView; CarouseViewPlus *carouseViewPlus; // 輪播圖相關的資料 NSArray *kvDataArray; } @property(nonatomic,strong) UITableView *tableview; @property (nonatomic , strong)UICollectionView *clv; @property (strong, nonatomic) UIView *contentView; @property (strong, nonatomic) UIButton *popBtn; @end @implementation twoViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"服務"; self.navigationController.navigationBar.titleTextAttributes= @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:22]}; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"22"] style:UIBarButtonItemStylePlain target:self action:nil]; [self.view addSubview:self.tableview]; [self setn]; } - (UITableView *)tableview{ if (!_tableview ){ _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain]; _tableview.delegate = self; _tableview.dataSource = self; } return _tableview; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return 1; }else if (section == 1){ return 2; }else{ return 4; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; // if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]; // if (indexPath.section == 0) { tableView.rowHeight = 150; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; // 格子的大小 layout.itemSize = CGSizeMake(50, 50); // 行間距 layout.minimumLineSpacing = 20; // 列間距 layout.minimumInteritemSpacing = 50; // 分割槽間距 layout.sectionInset = UIEdgeInsetsMake(15, 30, 30, 15); _clv = [[UICollectionView alloc]initWithFrame:CGRectMake(5, 5, self.view.frame.size.width, 150) collectionViewLayout:layout]; // 2.資料來源和代理 _clv.delegate = self; _clv.dataSource = self; // 3. 新增到主檢視 [_clv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuseCell"]; _clv.backgroundColor = [UIColor whiteColor]; [cell addSubview:_clv]; //'attempt to register a cell class which is not a subclass of UICollectionViewCell (UICollectionView)' }else if (indexPath.section == 1){ //第二分割槽 if (indexPath.row == 0) { //第二分割槽第一行 //cell.textLabel.text = @"第二分割槽第一行"; tableView.rowHeight = 40; cell.textLabel.text = @"旗艦店"; cell.detailTextLabel.text = @"檢視更多"; }else{ //第二行 tableView.rowHeight = 200; cell.backgroundColor = [UIColor lightGrayColor]; UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0.2, 149, 200)]; v1.backgroundColor = [UIColor whiteColor]; [cell addSubview:v1]; UILabel *l1 = [[UILabel alloc]initWithFrame:CGRectMake(31, 38, 120, 35)]; [l1 setText:@"熱銷商品"]; l1.textColor = [UIColor blueColor]; l1.font = [UIFont systemFontOfSize:25]; [v1 addSubview:l1]; UILabel *l2 = [[UILabel alloc]initWithFrame:CGRectMake(28, 58, 120, 60)]; [l2 setText:@"迪士尼小熊維尼系列"]; l2.textColor = [UIColor lightGrayColor]; l2.font = [UIFont systemFontOfSize:13]; [v1 addSubview:l2]; UIImageView *im = [[UIImageView alloc]initWithFrame:CGRectMake(31, 100, 80, 120)]; im.image = [UIImage imageNamed:@"111"]; [v1 addSubview:im]; // UIView *v2 = [[UIView alloc]initWithFrame:CGRectMake(150.2, 0.2, 263, 99.9)]; v2.backgroundColor = [UIColor whiteColor]; [cell addSubview:v2]; UILabel *l3 = [[UILabel alloc]initWithFrame:CGRectMake(31, 38, 120, 35)]; [l3 setText:@"熱門推薦"]; //l3.textColor = [UIColor blackColor]; l3.font = [UIFont systemFontOfSize:25]; [v2 addSubview:l3]; UILabel *l4 = [[UILabel alloc]initWithFrame:CGRectMake(28, 58, 120, 60)]; [l4 setText:@"平安招財貓系列"]; l4.textColor = [UIColor lightGrayColor]; l4.font = [UIFont systemFontOfSize:13]; [v2 addSubview:l4]; UIImageView *im1 = [[UIImageView alloc]initWithFrame:CGRectMake(180, 35, 60, 60)]; im1.image = [UIImage imageNamed:@"222"]; [v2 addSubview:im1]; // UIView *v3 = [[UIView alloc]initWithFrame:CGRectMake(150.2, 100.2, 263, 100)]; v3.backgroundColor = [UIColor whiteColor]; [cell addSubview:v3]; UILabel *ll = [[UILabel alloc]initWithFrame:CGRectMake(31, 10, 120, 20)]; [ll setText:@"黑金十二星座"]; //l3.textColor = [UIColor blackColor]; ll.font = [UIFont systemFontOfSize:17]; [v3 addSubview:ll]; UILabel *lll = [[UILabel alloc]initWithFrame:CGRectMake(28, 30, 80, 20)]; [lll setText:@"迷你矩形卡"]; lll.textColor = [UIColor lightGrayColor]; lll.font = [UIFont systemFontOfSize:13]; [v3 addSubview:lll]; UIImageView *im2 = [[UIImageView alloc]initWithFrame:CGRectMake(60, 50, 70, 40)]; im2.image = [UIImage imageNamed:@"333"]; [v3 addSubview:im2]; UILabel *vi = [[UILabel alloc]initWithFrame:CGRectMake(170, 10, 120, 20)]; [vi setText:@"迪士尼系列"]; //l3.textColor = [UIColor blackColor]; vi.font = [UIFont systemFontOfSize:17]; [v3 addSubview:vi]; UILabel *xx = [[UILabel alloc]initWithFrame:CGRectMake(170, 30, 80, 20)]; [xx setText:@"版權軟膠卡"]; xx.textColor = [UIColor lightGrayColor]; xx.font = [UIFont systemFontOfSize:13]; [v3 addSubview:xx]; UIImageView *im3 = [[UIImageView alloc]initWithFrame:CGRectMake(210, 50, 40, 40)]; im3.image = [UIImage imageNamed:@"444"]; [v3 addSubview:im3]; } }else{ if (indexPath.row == 0) { tableView.rowHeight = 40; cell.textLabel.text = @"合作景點套票"; cell.detailTextLabel.text = @"檢視更多"; }else if (indexPath.row == 1){ tableView.rowHeight = 130; UILabel *l1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 20, 60, 35)]; [l1 setText:@"鳥巢"]; l1.textColor = [UIColor blackColor]; l1.font = [UIFont systemFontOfSize:20]; [cell addSubview:l1]; UILabel *l2 = [[UILabel alloc]initWithFrame:CGRectMake(17, 40, 80, 55)]; [l2 setText:@"$ 106.00"]; l2.textColor = [UIColor yellowColor]; l2.font = [UIFont systemFontOfSize:16]; [cell addSubview:l2]; UILabel *l3 = [[UILabel alloc]initWithFrame:CGRectMake(85, 40, 85, 55)]; [l3 setText:@"$ 110.00"]; l3.textColor = [UIColor lightGrayColor]; l3.font = [UIFont systemFontOfSize:14]; [cell addSubview:l3]; UILabel *l4 = [[UILabel alloc]initWithFrame:CGRectMake(15, 80, 150, 55)]; [l4 setText:@" 套票108天后過期"]; l4.textColor = [UIColor blackColor]; l4.font = [UIFont systemFontOfSize:15]; [cell addSubview:l4]; UIImageView *imV = [[UIImageView alloc]initWithFrame:CGRectMake(240, 15,160, 100)]; imV.image = [UIImage imageNamed:@"666"]; [cell addSubview:imV]; }else if (indexPath.row == 2){ tableView.rowHeight = 130; UILabel *l1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 20, 120, 35)]; [l1 setText:@"十三陵神路"]; l1.textColor = [UIColor blackColor]; l1.font = [UIFont systemFontOfSize:20]; [cell addSubview:l1]; UILabel *l2 = [[UILabel alloc]initWithFrame:CGRectMake(17, 40, 80, 55)]; [l2 setText:@"$ 25.00"]; l2.textColor = [UIColor yellowColor]; l2.font = [UIFont systemFontOfSize:16]; [cell addSubview:l2]; UILabel *l3 = [[UILabel alloc]initWithFrame:CGRectMake(85, 40, 85, 55)]; [l3 setText:@"$ 30.00"]; l3.textColor = [UIColor lightGrayColor]; l3.font = [UIFont systemFontOfSize:14]; [cell addSubview:l3]; UILabel *l4 = [[UILabel alloc]initWithFrame:CGRectMake(15, 80, 150, 55)]; [l4 setText:@" 套票76天后過期"]; l4.textColor = [UIColor blackColor]; l4.font = [UIFont systemFontOfSize:15]; [cell addSubview:l4]; UIImageView *imV = [[UIImageView alloc]initWithFrame:CGRectMake(240, 15,160, 100)]; imV.image = [UIImage imageNamed:@"777"]; [cell addSubview:imV]; }else if(indexPath.row == 3){ tableView.rowHeight = 130; UILabel *l1 = [[UILabel alloc]initWithFrame:CGRectMake(15, 20, 120, 35)]; [l1 setText:@"十三陵定陵"]; l1.textColor = [UIColor blackColor]; l1.font = [UIFont systemFontOfSize:20]; [cell addSubview:l1]; UILabel *l2 = [[UILabel alloc]initWithFrame:CGRectMake(17, 40, 80, 55)]; [l2 setText:@"$ 52.00"]; l2.textColor = [UIColor yellowColor]; l2.font = [UIFont systemFontOfSize:16]; [cell addSubview:l2]; UILabel *l3 = [[UILabel alloc]initWithFrame:CGRectMake(85, 40, 85, 55)]; [l3 setText:@"$ 60.00"]; l3.textColor = [UIColor lightGrayColor]; l3.font = [UIFont systemFontOfSize:14]; [cell addSubview:l3]; UILabel *l4 = [[UILabel alloc]initWithFrame:CGRectMake(15, 80, 150, 55)]; [l4 setText:@" 套票76天后過期"]; l4.textColor = [UIColor blackColor]; l4.font = [UIFont systemFontOfSize:15]; [cell addSubview:l4]; UIImageView *imV = [[UIImageView alloc]initWithFrame:CGRectMake(240, 15,160, 100)]; imV.image = [UIImage imageNamed:@"888"]; [cell addSubview:imV]; } } return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section == 0) { return 130; }else{ return 20; } } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if (section == 0) { UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 130)]; // headerView.backgroundColor = [UIColor redColor]; kvDataArray = @[@"page 1", @"page 2", @"page3", @"page 4", @"page 5"]; UIImageView *v = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)]; v.image = [UIImage imageNamed:@"tongzhi"]; // 新增輪播圖1 carouseView = [[CarouseView alloc] init]; carouseView.frame = CGRectMake(0, 0, self.view.frame.size.width, 100); carouseView.datasource = self; carouseView.delegate = self; [headerView addSubview:v]; [headerView addSubview:carouseView]; return headerView; }else { return nil; } } // 分割槽個數(幾組) -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } // 每個分割槽有幾個item (小格子的個數) -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ if(section == 0){ return 8; }else{ return 2; } } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseCell" forIndexPath:indexPath]; // cell 新增背景色 cell.backgroundColor = [UIColor whiteColor]; // 'could not dequeue a view of kind: UICollectionElementKindCell with identifier 123 - must register a nib or a class for the identifier or // 80/100 NSArray *arr = @[@"33",@"44",@"55",@"66",@"77",@"88",@"99",@"10"]; NSArray *arr1 = @[@"旗艦店",@"線上客服",@"積分商城",@"交易記錄",@"市郊鐵路卡",@"學生卡",@"光大聯名卡",@"更多"]; UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; imgV.image = [UIImage imageNamed:arr[indexPath.row]]; [cell addSubview:imgV]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 70, 20)]; label.text = arr1[indexPath.row]; // 居中 // label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:12]; [cell addSubview:label]; return cell; } -(void)setn{ self.view.backgroundColor = [UIColor whiteColor]; _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)]; _contentView.backgroundColor = [UIColor clearColor]; _popBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _popBtn.frame = CGRectMake(0, 250, 200, 50); _popBtn.backgroundColor = [UIColor greenColor]; [_popBtn addTarget:self action:@selector(closeAndBack) forControlEvents:UIControlEventTouchUpInside]; UIImageView *imageV = [[UIImageView alloc]initWithFrame:_contentView.bounds]; imageV.image = [UIImage imageNamed:@"jei"]; [_contentView addSubview:imageV]; // 看看pop效果把下面這一句加上 // [_contentView addSubview:_popBtn]; [HWPopTool sharedInstance].shadeBackgroundType = ShadeBackgroundTypeSolid; [HWPopTool sharedInstance].closeButtonType = ButtonPositionTypeRight; [[HWPopTool sharedInstance] showWithPresentView:_contentView animated:YES]; } - (void)closeAndBack { [[HWPopTool sharedInstance] closeWithBlcok:^{ [self.navigationController popViewControllerAnimated:YES]; }]; } #pragma mark - 輪播圖代理 - (NSInteger)countOfCellForCarouseView:(CarouseView *)carouseView { return kvDataArray.count; } - (UIView *)carouselView:(CarouseView *)carouselView cellAtIndex:(NSInteger)index { // 先用空白頁測試 // UIView *imageView = [[UIView alloc] init]; // int R = (arc4random() % 256) ; // int G = (arc4random() % 256) ; // int B = (arc4random() % 256) ; // imageView.backgroundColor = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1]; // 填充view,可以是任意view UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg", (long)index + 1]]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)]; label.text = kvDataArray[index]; [imageView addSubview:label]; return imageView; } - (void)carouseView:(CarouseView *)carouseView didSelectedAtIndex:(NSInteger)index { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"carouse1 msg" message:kvDataArray[index] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; [alertView show]; } @end