表视图学习笔记

来源:互联网 发布:笔记本温度测试软件 编辑:程序博客网 时间:2024/05/16 09:54





//  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{

    [_arrrelease];

    [_tableView release];

    [superdealloc];

}


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

   if (self) {

        self.arr=[NSMutableArrayarrayWithObjects:@"宋江",@"卢俊义",@"吴用",@"公孙胜",@"关胜",@"林冲",@"秦明" ,@"呼延灼" ,@"花荣",@"柴进",@"李应",@"朱仝",@"鲁智深",@"武松",nil];

    }

    return self;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.translucent =NO;

 

    self.tableView =[[UITableViewalloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height-64)style:UITableViewStylePlain];

    self.tableView.backgroundColor =[UIColorredColor];

    [self.viewaddSubview:self.tableView];

    [self.tableViewrelease];

   self.tableView.rowHeight =100;

   self.tableView.delegate =self;

    self.tableView.dataSource=self;


    self.imageView =[[UIImageViewalloc] initWithImage:[UIImageimageNamed:[NSStringstringWithFormat:@"1.jpg"]]];

   self.imageView.frame =CGRectMake(0, -260,375, 260);

//    [self.view addSubview:self.imageView];

    [self.imageViewrelease];

    // tableView添加头视图

    // 宽是tableView的宽度

//    self.tableView.tableHeaderView =self.imageView;

    

    [self.tableViewaddSubview: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 =[tableViewdequeueReusableCellWithIdentifier:reuse];

   if (!cell) {

        cell =[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:reuse] autorelease];

        

    }

    cell.textLabel.text =self.arr[indexPath.row];

    cell.detailTextLabel.text =[NSStringstringWithFormat:@"%ld", indexPath.section];

    cell.imageView.image =[UIImageimageNamed:@"1.jpg"];

   return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        

        SecondViewController *secVC=[[SecondViewControlleralloc] init];

    

    secVC.name =self.arr[indexPath.row];

    

        [self.navigationControllerpushViewController:secVC animated:YES];

        [secVCrelease];

    // 5.设置代理人

    secVC.delegate =self;

        

 }

// 6.实现方法

-(void)change:(NSString *)value{

    //属性的数组,相当于数据源,把传过来的织田见到数组中

   if(![value isEqualToString:@""]){

    [self.arraddObject:value];

    //tableView进行刷新操作

    [self.tableViewreloadData];

    }

}



#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{

    [_labelrelease];

    [_textField release];

    [_buttonrelease];

    [_namerelease];

    [superdealloc];

}



- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor =[UIColoryellowColor];

    // 建一个 label

    self.label =[[UILabelalloc] initWithFrame:CGRectMake(100,100, 100, 30)];

    self.label.backgroundColor =[UIColorredColor];

    self.label.layer.borderWidth =1;

    self.label.layer.cornerRadius =5;

    self.label.layer.masksToBounds=YES;

    // label进行赋值

   self.label.text =self.name;

    [self.viewaddSubview:self.label];

    [self.labelrelease];

    // 建一个 textField

    self.textField =[[UITextFieldalloc] initWithFrame:CGRectMake(100,200, 100, 30)];

    self.textField.layer.borderWidth =1;

    self.textField.layer.cornerRadius =5;

    

    self.textField.backgroundColor =[UIColorcyanColor];

    

    [self.viewaddSubview:self.textField];

    [self.textFieldrelease];

    

    // 建一个 button

    self.button =[UIButtonbuttonWithType:UIButtonTypeSystem];

   self.button.frame=CGRectMake(100,300, 100, 30);

    [self.viewaddSubview:self.button];

    self.button.layer.borderWidth=1;

    self.button.layer.cornerRadius=5;

    [self.buttonsetTitle:@"返回"forState:UIControlStateNormal];

    self.button.backgroundColor =[UIColorredColor];

    [self.buttonaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];

   self.textField.delegate =self;

    

    

}


-(void)click:(UIButton *)button{

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

    [self.delegatechange:self.textField.text];


}

#pragma mark 点击return 回收键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    

    [self.textFieldendEditing:YES];

    return YES;

}




0 0
原创粉丝点击