码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UI08_TableView界面传值

时间:2015-08-08 12:08:41      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:界面   传值   

实现两个界面之间内容的传递

//
//  MainViewController.m
//  UI08_TableView界面传值
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)UIImageView *imageView;
@end

@implementation MainViewController
-(void)dealloc
{
    [_arr release];
    [_imageView release];
    [_tableView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =[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.navigationItem.title=@"表视图";
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.rowHeight=50;

    self.tableView.dataSource=self;
    self.tableView.delegate=self;

    self.imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"h1.jpeg"]];
    self.imageView.frame=CGRectMake(0, -200, self.view.frame.size.width, 200);
    //给tableview添加头视图
    //宽是tableview的宽度
//    self.tableView.tableHeaderView=self.imageView;
    [self.tableView addSubview:self.imageView];

    self.tableView.contentInset=UIEdgeInsetsMake(200, 0, 0, 0);



}
#pragma mark tableview的delegate已经签订好scrollerView的协议,只要设置代理人,就可以使用scrollerview的协议方法
//只要滑动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   //获取偏移量
    CGFloat y=scrollView.contentOffset.y;
    NSLog(@"%g",y);
    if (y<0) {
        self.imageView.frame=CGRectMake(0, y, self.view.frame.size.width, -y);
    }
}



-(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:@"天平.png"];
    return cell;

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"水浒";
}



- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.arr;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (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];
    //


    secVC.delegate=self;


}

-(void)changeValue:(NSString *)value
{
    //属性的数组,相当于数据源,把传过来的值添加到数组中
    [self.arr addObject:value];
    //对数据进行刷新
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  SecondViewController.h
//  UI08_TableView界面传值
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>

//协议方法
-(void)changeValue:(NSString *)value;

@end
@interface SecondViewController : UIViewController
@property(nonatomic,copy)NSString *name;

@property(nonatomic,assign)id<SecondViewControllerDelegate>delegate;

@end
//
//  SecondViewController.m
//  UI08_TableView界面传值
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()
@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 cyanColor];

    self.label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
    self.label.backgroundColor=[UIColor redColor];
    self.label.layer.borderWidth=1;
    self.label.layer.cornerRadius=10;
    [self.view addSubview:self.label];
    //赋值
    self.label.text=self.name;
    [self.label release];



    self.textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 40)];
    self.textfield.layer.borderWidth=1;
    self.textfield.layer.cornerRadius=10;
    [self.view addSubview:self.textfield];
    [self.textfield release];


    self.button=[UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame=CGRectMake(200, 300, 150, 30);
    [self.button setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    self.button.layer.borderWidth=1;
    self.button.layer.cornerRadius=5;
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

}
-(void)click:(UIButton *)button
{
    [self.delegate changeValue:self.textfield.text];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS UI08_TableView界面传值

标签:界面   传值   

原文地址:http://blog.csdn.net/cheng_xiansheng/article/details/47356227

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!