当前位置:首页 > 操作系统 > Ios

iOS 课程表 源码 实例

思路按照起始点位置和宽高比例进行button 的添加
1// 2// TableController.m 3// hedaAssistant 4// 5// Created by bear on 15/11/28. 6// Copyright ? 2015年 bear. All rights reserved. 7// 8 9 #import "TableController.h" 10 #import "TableCellController.h" 11 #import "UINavigationItem+custom.h" 12 13 14//获取屏幕 宽度、高度 15#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) 16#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) 17 18 19//NavBar高度 20#define NavigationBar_HEIGHT 44 21#define StatusBar_HEIGHT 20 22#define TabBar_HEIGHT 49 23 24@interface TableController (){ 25 UIScrollView *tableScrowView; 26 CGFloat dayWidth; 27 CGFloat sectionHeight; 28 29}@end 30 31@implementation TableController 32 -(void)viewDidLoad{ 33 34 35 [self setUI]; 36 37} 38 39 40 41 -(void)setUI{ 42 43 44//设置 导航栏的背景 45 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tabbar_background.png"] forBarMetrics:UIBarMetricsDefault]; 46 47//设置自己的标题x 48 [self.navigationItem setMyTitle:@"考试安排表"]; 49 50 51 52 53 54 55//添加整体背景 56 57 UIImage *bgImg=[UIImage imageNamed:@"main.png"]; 58 UIImageView *mainBgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-(StatusBar_HEIGHT+NavigationBar_HEIGHT+TabBar_HEIGHT))]; 59 mainBgView.image=bgImg; 60 [self.view addSubview:mainBgView]; 61 62//添加顶部星期背景 63 UIView *headBg=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)]; 64 [headBg setAlpha:0.7f]; 65 headBg.backgroundColor=[UIColor orangeColor]; 66 [self.view addSubview:headBg]; 67 68 69//定义基本宽高 70 dayWidth=(CGFloat) (SCREEN_WIDTH-30)/5; 71 sectionHeight=(CGFloat)(SCREEN_HEIGHT-(StatusBar_HEIGHT+NavigationBar_HEIGHT+TabBar_HEIGHT)-30)/8; 72 73 74#pragma mark 添加ScrowView能左右滚动 75 tableScrowView=[[UIScrollView alloc]init]; 76 tableScrowView.frame=CGRectMake(0, 0, SCREEN_WIDTH,sectionHeight*8+35); 77 tableScrowView.contentSize = CGSizeMake(dayWidth*7+30, 0); 78 tableScrowView.bounces=NO; 79 [self.view addSubview:tableScrowView]; 80 81 82#pragma mark 添加星期头部 83 84int i; 85 NSString *weektitle[]={@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",@"星期日"}; 86for(i =0; i<7;i++) 87 { 88 UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 89//为button显示赋值 90 91 [buttonview setTitle:weektitle[i] forState:UIControlStateNormal]; 92 [buttonview setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 93//设置button的大小 94 buttonview.frame = CGRectMake(30+i*dayWidth, 0, dayWidth, 30); 95 [tableScrowView addSubview:buttonview]; 96 97 98 99100 } 101102103#pragma mark 添加左边节数 标示 104 UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 30, 30, sectionHeight*8)]; 105 [self.view addSubview:sectionView]; 106for(i =0; i<8;i++) 107 { 108//新建一个button对象 button还有一些别的属性比如背景色109 NSString *title=[NSString stringWithFormat:@"%d",i+1]; 110 UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 111//为button显示赋值112 buttonview.backgroundColor=[UIColor lightGrayColor]; 113if (i%2) { 114 [buttonview setAlpha:0.5f]; 115 }else{ 116 [buttonview setAlpha:0.7f]; 117 } 118119 [buttonview setTitle:title forState:UIControlStateNormal]; 120 [buttonview setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 121//设置button的大小122 buttonview.frame = CGRectMake(0, i*sectionHeight, 30, sectionHeight); 123 [sectionView addSubview:buttonview]; 124125126 } 127128129130#pragma mark 加号分割点添加 131int j; 132for (i=0; i<8; i++) { 133for (j=0; j<7; j++) { 134 UILabel *lable=[[UILabel alloc]init]; 135 [lable setText:@"+"]; 136 [lable setTextColor:[UIColor lightGrayColor]]; 137 lable.frame=CGRectMake(30+dayWidth*i, 30+sectionHeight*j, 10, 10); 138 lable.center=CGPointMake(30+dayWidth*i, sectionHeight*(j+1.5)); 139 [tableScrowView addSubview:lable]; 140 } 141 } 142143144145146147148149150//添加Lesson 151 [self addLesson]; 152153} 154155156157158#pragma mark 画课表按钮 每节课按照数组画出来 159 -(void)addLesson{ 160161162// 以下为测试数据163 UIButton *lessonBUtton=[[UIButton alloc]initWithFrame:CGRectMake(30, 30, dayWidth, sectionHeight*2)]; 164 [lessonBUtton setTitle:@"ios程序设计" forState:UIControlStateNormal]; 165 lessonBUtton.titleLabel.numberOfLines=0; 166 [lessonBUtton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 167 [lessonBUtton setBackgroundColor:[UIColor redColor]]; 168 [lessonBUtton.layer setCornerRadius:8.0]; 169 [lessonBUtton setAlpha:0.7f]; 170 [lessonBUtton addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 171 [tableScrowView addSubview:lessonBUtton]; 172173174175 UIButton *lessonBUtton1=[[UIButton alloc]initWithFrame:CGRectMake(30, 30+sectionHeight*4, dayWidth, sectionHeight*2)]; 176 [lessonBUtton1 setTitle:@"专业英语" forState:UIControlStateNormal]; 177 lessonBUtton1.titleLabel.numberOfLines=0; 178 [lessonBUtton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 179 [lessonBUtton1 setBackgroundColor:[UIColor purpleColor]]; 180 [lessonBUtton1.layer setCornerRadius:8.0]; 181 [lessonBUtton1 setAlpha:0.7f]; 182 [lessonBUtton1 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 183 [tableScrowView addSubview:lessonBUtton1]; 184185186 UIButton *lessonBUtton12=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*2, 30+sectionHeight*3, dayWidth, sectionHeight*2)]; 187 [lessonBUtton12 setTitle:@"概率论" forState:UIControlStateNormal]; 188 lessonBUtton12.titleLabel.numberOfLines=0; 189 [lessonBUtton12 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 190 [lessonBUtton12 setBackgroundColor:[UIColor blueColor]]; 191 [lessonBUtton12.layer setCornerRadius:8.0]; 192 [lessonBUtton12 setAlpha:0.7f]; 193 [lessonBUtton12 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 194 [tableScrowView addSubview:lessonBUtton12]; 195196197 UIButton *lessonBUtton135=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*4, 30+sectionHeight*0, dayWidth, sectionHeight*3)]; 198 [lessonBUtton135 setTitle:@"疯玩,疯睡。哈哈哈" forState:UIControlStateNormal]; 199 lessonBUtton135.titleLabel.numberOfLines=0; 200 [lessonBUtton135 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 201 [lessonBUtton135 setBackgroundColor:[UIColor brownColor]]; 202 [lessonBUtton135.layer setCornerRadius:8.0]; 203 [lessonBUtton135 setAlpha:0.7f]; 204 [lessonBUtton135 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 205 [tableScrowView addSubview:lessonBUtton135]; 206207208209 UIButton *lessonBUtton15=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*5, 30+sectionHeight*1, dayWidth, sectionHeight*2)]; 210 [lessonBUtton15 setTitle:@"专业英语" forState:UIControlStateNormal]; 211 lessonBUtton15.titleLabel.numberOfLines=0; 212 [lessonBUtton15 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 213 [lessonBUtton15 setBackgroundColor:[UIColor purpleColor]]; 214 [lessonBUtton15.layer setCornerRadius:8.0]; 215 [lessonBUtton15 setAlpha:0.7f]; 216 [lessonBUtton15 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 217 [tableScrowView addSubview:lessonBUtton15]; 218219 UIButton *lessonBUtton123=[[UIButton alloc]initWithFrame:CGRectMake(30+dayWidth*6, 30+sectionHeight*5, dayWidth, sectionHeight*2)]; 220 [lessonBUtton123 setTitle:@"睡觉,不写了" forState:UIControlStateNormal]; 221 lessonBUtton123.titleLabel.numberOfLines=0; 222 [lessonBUtton123 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 223 [lessonBUtton123 setBackgroundColor:[UIColor orangeColor]]; 224 [lessonBUtton123.layer setCornerRadius:8.0]; 225 [lessonBUtton123 setAlpha:0.7f]; 226 [lessonBUtton123 addTarget:self action:@selector(cellClick:) forControlEvents:UIControlEventTouchUpInside]; 227 [tableScrowView addSubview:lessonBUtton123]; 228229230//231232// 此处循环创建的Button 可由tag 取出 233// for (int i=0 ; i<6; i++) { 234// UIButton *button=[UIButton alloc]; 235// button.tag=4; 236// [tableScrowView addSubview:button]; 237//238//239// } 240// 241242243} 244245246247248 -(void)cellClick:(UIButton*) _button{ 249250 TableCellController *tableCell=[[TableCellController alloc]init]; 251252 tableCell.button=_button; 253 UINavigationController *navTableCell=[[UINavigationController alloc]initWithRootViewController:tableCell]; 254255 self.view.window.rootViewController = navTableCell; 256257258259} 260261262//263//TableCellController *tableCell=[[TableCellController alloc]init]; 264//265//tableCell.button=_button; 266//UINavigationController *navTableCell=[[UINavigationController alloc]initWithRootViewController:tableCell]; 267//[UIView transitionFromView:self.view.window.rootViewController.view 268// toView:navTableCell.view 269// duration:0.5 270// options:UIViewAnimationOptionTransitionCurlUp 271// completion:^(BOOL finished) 272// { 273// self.view.window.rootViewController = navTableCell; 274// }];275276277278 @end

源码下载地址     https://github.com/xiaocaiabc/hedaAssistant.git

原文:http://www.cnblogs.com/xiaocaiabc/p/5125541.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!