标签:
// MainViewController.m
// UI_TableView界面传值
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import "MainViewController.h"
#import "SecondViewController.h"
// 4.签订协议
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewController>
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic ,retain)UIImageView *imageView;
@end
@implementation MainViewController
-(void)dealloc{
[_arr release];
[_tableView release];
[super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr=[NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent =NO;
self.tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
self.tableView.backgroundColor =[UIColor redColor];
[self.view addSubview:self.tableView];
[self.tableView release];
self.tableView.rowHeight =100;
self.tableView.delegate =self;
self.tableView.dataSource=self;
self.imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]]];
self.imageView.frame =CGRectMake(0, -260, 375, 260);
// [self.view addSubview:self.imageView];
[self.imageView release];
// 给tableView添加头视图
// 宽是tableView的宽度
// self.tableView.tableHeaderView =self.imageView;
[self.tableView addSubview:self.imageView];
self.tableView.contentInset =UIEdgeInsetsMake(260, 0, 0, 0);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuse =@"reuse";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text =self.arr[indexPath.row];
cell.detailTextLabel.text =[NSString stringWithFormat:@"%ld", indexPath.section];
cell.imageView.image =[UIImage imageNamed:@"1.jpg"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecondViewController *secVC=[[SecondViewController alloc] init];
secVC.name =self.arr[indexPath.row];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
// 5.设置代理人
secVC.delegate =self;
}
// 6.实现方法
-(void)change:(NSString *)value{
// 属性的数组,相当于数据源,把传过来的织田见到数组中
if(![value isEqualToString:@""]){
[self.arr addObject:value];
//对tableView进行刷新操作
[self.tableView reloadData];
}
}
#pragma mark tableView 的delegate已经签订好scrollView的协议,只要设置代理人,就可以使用scrollView的协议方法
// 只要滑动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"滑动");
// 获取偏移量
CGFloat y =scrollView.contentOffset.y;
NSLog(@"%g",y);
if (y<-260) {
self.imageView.frame =CGRectMake(0, y, self.view.frame.size.width, -y);
}
}
// SecondViewController.h
// UI_TableView界面传值
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <UIKit/UIKit.h>
// 声明一份协议
@protocol SecondViewController <NSObject>
-(void)change:(NSString *)value;
@end
@interface SecondViewController : UIViewController
@property(nonatomic,copy)NSString *name;
// 设置代理人属性
@property(nonatomic ,assign)id<SecondViewController>delegate;
@end
// SecondViewController.m
// UI_TableView界面传值
//
// Created by dllo on 15/8/7.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()<UITextFieldDelegate>
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UITextField *textField;
@property(nonatomic ,retain)UIButton *button;
@end
@implementation SecondViewController
-(void)dealloc{
[_label release];
[_textField release];
[_button release];
[_name release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor =[UIColor yellowColor];
// 建一个 label
self.label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
self.label.backgroundColor =[UIColor redColor];
self.label.layer.borderWidth =1;
self.label.layer.cornerRadius =5;
self.label.layer.masksToBounds=YES;
// 对label进行赋值
self.label.text =self.name;
[self.view addSubview:self.label];
[self.label release];
// 建一个 textField
self.textField =[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 100, 30)];
self.textField.layer.borderWidth =1;
self.textField.layer.cornerRadius =5;
self.textField.backgroundColor =[UIColor cyanColor];
[self.view addSubview:self.textField];
[self.textField release];
// 建一个 button
self.button =[UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame=CGRectMake(100, 300, 100, 30);
[self.view addSubview:self.button];
self.button.layer.borderWidth=1;
self.button.layer.cornerRadius=5;
[self.button setTitle:@"返回" forState:UIControlStateNormal];
self.button.backgroundColor =[UIColor redColor];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
self.textField.delegate =self;
}
-(void)click:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
[self.delegate change:self.textField.text];
}
#pragma mark 点击return 回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self.textField endEditing:YES];
return YES;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/mltianya/article/details/47344733