0、创建HelloWorld工程,在ViewController.m文件
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//设置背景色
self.view.backgroundColor = [UIColor whiteColor];
}
文本控件UILabel
在
//设置背景色
self.view.backgroundColor = [UIColor whiteColor];
后添加以下代码块:
UILabel *label = [[UILabel alloc]init];//新建一个对象,相当于Java的new
label.text =@"你好,世界";//设置要显示的文本
[label setFrame:CGRectMake(0, 0, 200, 50)];//设置所在的位置
[self.view addSubview:label];//添加该View
多行文字自动换行
bottomLabel.lineBreakMode = NSLineBreakByWordWrapping;
bottomLabel.numberOfLines = 0;
bottomLabel.preferredMaxLayoutWidth = SCREEN_WIDTH;
按钮控件UIButton
UIButton *btn = [[UIButton alloc] init];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:@"坚持100天" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(0, 0, 200, 50)];
[self.view addSubview:btn];
//添加点击事件
[btn addTarget:self action:@selector(clickEvent) forControlEvents: UIControlEventTouchUpInside];
-(void) clickEvent{
NSLog(@"你哈");
//弹出对话框
[self showDialog:@"我是点击响应事件!!!!!"];
}
弹出对话框
-(void) showDialog:(NSString *)dialogContent{
UIAlertController* dialog = [UIAlertController alertControllerWithTitle:@"提示" message:dialogContent preferredStyle:UIAlertControllerStyleAlert];
//t
[dialog addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
[dialog addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
//
[self presentViewController:dialog animated:true completion:nil];
}
列表
#import "ViewController.h"
#import "Masonry.h"
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define STATUS_HEIGHT ([[UIApplication sharedApplication] statusBarFrame].size.height)
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *data;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//设置背景色
self.view.backgroundColor = [UIColor whiteColor];
//标题栏
UIView *titleView =[[UIView alloc]init];
titleView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:titleView];
[titleView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).with.offset(STATUS_HEIGHT);
make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
}];
//
UIButton *btn = [[UIButton alloc] init];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn setTitle:@"更多" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1] forState:UIControlStateNormal];
NSLog(@"高度:%f",STATUS_HEIGHT);
//[btn setFrame:CGRectMake(0, STATUS_HEIGHT, SCREEN_WIDTH, 50)];
[self.view addSubview:btn];
//
[btn mas_makeConstraints:^(MASConstraintMaker *make){
make.centerY.equalTo(titleView);
make.size.mas_equalTo(CGSizeMake(80, 50));
make.right.mas_equalTo(self.view.mas_right);
}];
[btn addTarget:self action:@selector(click) forControlEvents: UIControlEventTouchUpInside];
//
UILabel *label_title = [[UILabel alloc]init];
label_title.text = @"坚持100天";
label_title.textColor = [UIColor blackColor];
[titleView addSubview:label_title];
[label_title mas_makeConstraints:^(MASConstraintMaker *make){
make.center.equalTo(titleView);
}];
//
NSArray *pro = [[NSArray alloc] initWithObjects:@"玩臂力棒",@"背单词",@"跑步",@"编程",@"玩臂力棒2",@"背单词2",@"跑步2",@"编程2", nil];
self.data = pro;
//
/**
* 表格视图样式
* UITableViewStylePlain //无分区样式
UITableViewStyleGrouped //分区样式
*/
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64) style:UITableViewStylePlain];
/**
* 设置代理
*/
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
}
-(void)click{
NSLog(@"你哈");
//
[self showDialog:@"我是陈贵坚!!!!!"];
//[self presentViewController:MyView animated:true completion:nil];
//self.window.RootViewController = MyView;
}
-(void) showDialog:(NSString *)dialogContent{
UIAlertController* dialog = [UIAlertController alertControllerWithTitle:dialogContent message:@"" preferredStyle:UIAlertControllerStyleAlert];
//t
[dialog addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
[dialog addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
//
[self presentViewController:dialog animated:true completion:nil];
}
#pragma mark - UITableViewDataSource
/**
* 有多少个分区
* UITableViewDataSource协议中的所有方法,用来对表格视图的样式进行设置(比如说显示的分区个数、每个分区中单元格个数、单元格中显示内容、分区头标题、分区未标题、分区的View)
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
/**
* 设置每个分区显示的行数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return [self.data count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
///<1.>设置标识符
static NSString * str = @"cellStr";
///<2.>复用机制:如果一个页面显示7个cell,系统则会创建8个cell,当用户向下滑动到第8个cell时,第一个cell进入复用池等待复用。
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
///<3.>新建cell
if (cell == nil) {
//副标题样式
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
}
///<4.>设置单元格上显示的文字信息
if (indexPath.section == 0) {
cell.textLabel.text = [NSString stringWithFormat:@"%@",[self.data objectAtIndex:indexPath.row]];
}
return cell;
}
/**
* 由于表格的样式设置成plain样式,但是不能说明当前的表格不显示分区
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
/*
if (section == 0) {
return @"分区一";
}else {
return @"分区二";
}
*/
return @"";
}
/**
* 设置单元格的高度
* 单元格默认高度44像素
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0f;
}
/**
* UITableViewDelegate协议中所有方法,用来对单元格自身进行操作(比如单元格的删除、添加、移动...)
*/
#pragma UITableViewDelegate
/**
* 点击单元格触发该方法
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selectCellStr = cell.textLabel.text;
//
[self showDialog:selectCellStr];
}
@end
网友评论